Skip to content
Permalink
Browse files

Add source link to listing directive

  • Loading branch information
ralsina committed Aug 28, 2016
1 parent 8eafc13 commit c227e8930ff513f8f4f7b9639ef24bc132fc3888
Showing with 22 additions and 1 deletion.
  1. +5 −1 nikola/plugins/compile/rest/listing.py
  2. +17 −0 nikola/plugins/task/listings.py
@@ -136,6 +136,7 @@ def set_site(self, site):
# leaving these to make the code directive work with
# docutils < 0.9
CodeBlock.site = site
Listing.site = site
directives.register_directive('code', CodeBlock)
directives.register_directive('code-block', CodeBlock)
directives.register_directive('sourcecode', CodeBlock)
@@ -189,8 +190,11 @@ def run(self):
self.content = fileobject.read().splitlines()
self.state.document.settings.record_dependencies.add(fpath)
target = urlunsplit(("link", 'listing', fpath.replace('\\', '/'), '', ''))
src_target = urlunsplit(("link", 'listing-source', fpath.replace('\\', '/'), '', ''))
src_label = self.site.MESSAGES('Source')
generated_nodes = (
[core.publish_doctree('`{0} <{1}>`_'.format(_fname, target))[0]])
[core.publish_doctree('`{0} <{1}>`_ `({2}) <{3}>`_' .format(
_fname, target, src_label, src_target))[0]])
generated_nodes += self.get_code_from_file(fileobject)
return generated_nodes

@@ -54,6 +54,7 @@ def register_output_name(self, input_folder, rel_name, rel_output_name):
def set_site(self, site):
"""Set Nikola site."""
site.register_path_handler('listing', self.listing_path)
site.register_path_handler('listing-source', self.listing_source_path)

# We need to prepare some things for the listings path handler to work.

@@ -255,6 +256,22 @@ def render_listing(in_name, out_name, input_folder, output_folder, folders=[], f
'clean': True,
}, self.kw["filters"])

def listing_source_path(self, name, lang):
"""A link to the source code for a listing.
It will try to use the file name if it's not ambiguous, or the file path.
Example:
link://listing-source/hello.py => /listings/tutorial/hello.py
link://listing-source/tutorial/hello.py => /listings/tutorial/hello.py
"""
result = self.listing_path(name, lang)
if result[-1].endswith('.html'):
result[-1] = result[-1][:-5]
return result

def listing_path(self, namep, lang):
"""A link to a listing.

0 comments on commit c227e89

Please sign in to comment.