-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
very basic listing plugin, needs work
- Loading branch information
Roberto Alsina
committed
Jul 24, 2017
1 parent
58e847d
commit 0f0670b
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,13 @@ | |||
[Core] | |||
name = listing_shortcode | |||
module = listing | |||
|
|||
[Nikola] | |||
PluginCategory = Shortcode | |||
|
|||
[Documentation] | |||
author = Roberto Alsina | |||
version = 0.1 | |||
website = https://getnikola.com/ | |||
description = Listing shortcode | |||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,46 @@ | |||
# -*- coding: utf-8 -*- | |||
# This file is public domain according to its author, Brian Hsu | |||
|
|||
"""Listing Shortcode""" | |||
|
|||
import os | |||
|
|||
import pygments | |||
|
|||
from nikola.plugin_categories import ShortcodePlugin | |||
|
|||
|
|||
class Plugin(ShortcodePlugin): | |||
"""Plugin for listing shortcode.""" | |||
|
|||
name = "listing" | |||
|
|||
def set_site(self, site): | |||
"""Set Nikola site.""" | |||
self.site = site | |||
# Even though listings don't use CodeBlock anymore, I am | |||
# leaving these to make the code directive work with | |||
# docutils < 0.9 | |||
Plugin.folders = site.config['LISTINGS_FOLDERS'] | |||
return super(Plugin, self).set_site(site) | |||
|
|||
def handler(self, fname, language='text', filename=None, site=None, data=None, lang=None, post=None): | |||
"""Create HTML for a listing.""" | |||
|
|||
fname = fname.replace('/', os.sep) | |||
if len(self.folders) == 1: | |||
listings_folder = next(iter(self.folders.keys())) | |||
if fname.startswith(listings_folder): | |||
fpath = os.path.join(fname) # new syntax: specify folder name | |||
else: | |||
fpath = os.path.join(listings_folder, fname) # old syntax: don't specify folder name | |||
else: | |||
fpath = os.path.join(fname) # must be new syntax: specify folder name | |||
|
|||
deps = [] | |||
with open(fpath, 'r') as inf: | |||
data = inf.read() | |||
lexer = pygments.lexers.get_lexer_by_name(language) | |||
formatter = pygments.formatters.get_formatter_by_name('html') | |||
output = pygments.highlight(data, lexer, formatter) | |||
return output, deps |