aboutsummaryrefslogtreecommitdiffstats
path: root/nikola/plugins/compile/rest
diff options
context:
space:
mode:
Diffstat (limited to 'nikola/plugins/compile/rest')
-rw-r--r--nikola/plugins/compile/rest/__init__.py43
-rw-r--r--nikola/plugins/compile/rest/listing.py19
2 files changed, 55 insertions, 7 deletions
diff --git a/nikola/plugins/compile/rest/__init__.py b/nikola/plugins/compile/rest/__init__.py
index 50b37cf..9a4e19b 100644
--- a/nikola/plugins/compile/rest/__init__.py
+++ b/nikola/plugins/compile/rest/__init__.py
@@ -43,7 +43,7 @@ except ImportError:
try:
from collections import OrderedDict
except ImportError:
- OrderedDict = None # NOQA
+ OrderedDict = dict # NOQA
from nikola.plugin_categories import PageCompiler
from nikola.utils import get_logger, makedirs, req_missing
@@ -102,11 +102,8 @@ class CompileRest(PageCompiler):
else:
return False
- def create_post(self, path, onefile=False, **kw):
- if OrderedDict is not None:
- metadata = OrderedDict()
- else:
- metadata = {}
+ def create_post(self, path, onefile=False, is_page=False, **kw):
+ metadata = OrderedDict()
metadata.update(self.default_metadata)
metadata.update(kw)
makedirs(os.path.dirname(path))
@@ -114,7 +111,7 @@ class CompileRest(PageCompiler):
if onefile:
for k, v in metadata.items():
fd.write('.. {0}: {1}\n'.format(k, v))
- fd.write("\nWrite your post here.")
+ fd.write("\nWrite your {0} here.".format('page' if is_page else 'post'))
def set_site(self, site):
for plugin_info in site.plugin_manager.getPluginsOfCategory("RestExtension"):
@@ -174,6 +171,38 @@ class NikolaReader(docutils.readers.standalone.Reader):
def add_node(node, visit_function=None, depart_function=None):
+ """
+ Register a Docutils node class.
+ This function is completely optional. It is a same concept as
+ `Sphinx add_node function <http://sphinx-doc.org/ext/appapi.html#sphinx.application.Sphinx.add_node>`_.
+
+ For example::
+
+ class Plugin(RestExtension):
+
+ name = "rest_math"
+
+ def set_site(self, site):
+ self.site = site
+ directives.register_directive('math', MathDirective)
+ add_node(MathBlock, visit_Math, depart_Math)
+ return super(Plugin, self).set_site(site)
+
+ class MathDirective(Directive):
+ def run(self):
+ node = MathBlock()
+ return [node]
+
+ class Math(docutils.nodes.Element): pass
+
+ def visit_Math(self, node):
+ self.body.append(self.starttag(node, 'math'))
+
+ def depart_Math(self, node):
+ self.body.append('</math>')
+
+ For full example, you can refer to `Microdata plugin <http://plugins.getnikola.com/#microdata>`_
+ """
docutils.nodes._add_node_class_names([node.__name__])
if visit_function:
setattr(docutils.writers.html4css1.HTMLTranslator, 'visit_' + node.__name__, visit_function)
diff --git a/nikola/plugins/compile/rest/listing.py b/nikola/plugins/compile/rest/listing.py
index ecf885f..d70e02d 100644
--- a/nikola/plugins/compile/rest/listing.py
+++ b/nikola/plugins/compile/rest/listing.py
@@ -56,6 +56,18 @@ except ImportError: # docutils < 0.9 (Debian Sid For The Loss)
from nikola.plugin_categories import RestExtension
+# Add sphinx compatibility option
+CodeBlock.option_spec['linenos'] = directives.unchanged
+
+
+class FlexibleCodeBlock(CodeBlock):
+
+ def run(self):
+ if 'linenos' in self.options:
+ self.options['number-lines'] = self.options['linenos']
+ return super(FlexibleCodeBlock, self).run()
+CodeBlock = FlexibleCodeBlock
+
class Plugin(RestExtension):
@@ -71,6 +83,10 @@ class Plugin(RestExtension):
directives.register_directive('listing', Listing)
return super(Plugin, self).set_site(site)
+# Add sphinx compatibility option
+listing_spec = Include.option_spec
+listing_spec['linenos'] = directives.unchanged
+
class Listing(Include):
""" listing directive: create a highlighted block of code from a file in listings/
@@ -84,6 +100,7 @@ class Listing(Include):
has_content = False
required_arguments = 1
optional_arguments = 1
+ option_spec = listing_spec
def run(self):
fname = self.arguments.pop(0)
@@ -91,6 +108,8 @@ class Listing(Include):
fpath = os.path.join('listings', fname)
self.arguments.insert(0, fpath)
self.options['code'] = lang
+ if 'linenos' in self.options:
+ self.options['number-lines'] = self.options['linenos']
with codecs_open(fpath, 'rb+', 'utf8') as fileobject:
self.content = fileobject.read().splitlines()
self.state.document.settings.record_dependencies.add(fpath)