Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add source link to listing directive
  • Loading branch information
ralsina committed Aug 28, 2016
1 parent 8eafc13 commit c227e89
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
6 changes: 5 additions & 1 deletion nikola/plugins/compile/rest/listing.py
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions nikola/plugins/task/listings.py
Expand Up @@ -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.

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit c227e89

Please sign in to comment.