Repository: getnikola/nikola

Issue:
rest plugins should know which page is being compiled
I want to access some of the metadata of the current page from a rest plugin and I can't find a way to get the current page in self.state.

I found out that by adding a reference to the "source" path when calling the rest compiler, I can retrieve it as self.state.document.settings._source, then find a matching page. Is it a good solution? Could this (or something similar) be integrated in the default rest compiler?



Discussion:
Proposed solution: https://github.com/aurelien-naldi/nikola/commit/2964a35f36c59973631c8d1d71e4449e640ae4cb

> ```
>  l_source=source,
> ```

We’d need to rename this to source_path, but we need to make sure that it won’t collide.

Indeed the source path is already given as argument, but is this available for the plugin afterwards?
If there is some way to get my plugin t work without a patched version of nikola, that would be perfect!

rst2html has this instruction:    pub.set_source(source, source_path)
but source_path takes always has value "None", putting l_source here would work also.

This also works for me: https://github.com/aurelien-naldi/nikola/commit/62bff7b008ffefbd6c8a30a7f9cf5b3f1b6c0432

Looks good.  Add yourself to /AUTHORS.txt and create a pull request.


Resolving patch:
diff --git a/nikola/plugins/compile/rest/__init__.py b/nikola/plugins/compile/rest/__init__.py
--- a/nikola/plugins/compile/rest/__init__.py
+++ b/nikola/plugins/compile/rest/__init__.py
@@ -83,7 +83,7 @@ def compile_html(self, source, dest, is_two_file=True):
                         'syntax_highlight': 'short',
                         'math_output': 'mathjax',
                         'template': default_template_path,
-                    }, logger=self.logger, l_source=source, l_add_ln=add_ln)
+                    }, logger=self.logger, source_path=source, l_add_ln=add_ln)
                 out_file.write(output)
             deps_path = dest + '.dep'
             if deps.list:
@@ -213,7 +213,7 @@ def rst2html(source, source_path=None, source_class=docutils.io.StringInput,
              parser=None, parser_name='restructuredtext', writer=None,
              writer_name='html', settings=None, settings_spec=None,
              settings_overrides=None, config_section=None,
-             enable_exit_status=None, logger=None, l_source='', l_add_ln=0):
+             enable_exit_status=None, logger=None, l_add_ln=0):
     """
     Set up & run a `Publisher`, and return a dictionary of document parts.
     Dictionary keys are the names of parts, and values are Unicode strings;
@@ -237,7 +237,7 @@ def rst2html(source, source_path=None, source_class=docutils.io.StringInput,
         # logger    a logger from Nikola
         # source   source filename (docutils gets a string)
         # add_ln   amount of metadata lines (see comment in compile_html above)
-        reader.l_settings = {'logger': logger, 'source': l_source,
+        reader.l_settings = {'logger': logger, 'source': source_path,
                              'add_ln': l_add_ln}
 
     pub = docutils.core.Publisher(reader, parser, writer, settings=settings,
@@ -246,7 +246,8 @@ def rst2html(source, source_path=None, source_class=docutils.io.StringInput,
     pub.set_components(None, parser_name, writer_name)
     pub.process_programmatic_settings(
         settings_spec, settings_overrides, config_section)
-    pub.set_source(source, source_path)
+    pub.set_source(source, None)
+    pub.settings._nikola_source_path = source_path
     pub.set_destination(None, destination_path)
     pub.publish(enable_exit_status=enable_exit_status)
 


Test patch: