Skip to content
Permalink
Browse files

very basic listing plugin, needs work

  • Loading branch information
ralsina committed Jul 24, 2017
1 parent 58e847d commit 0f0670b61e7e09e234fab097e8d93bfb0bb99b35
Showing with 59 additions and 0 deletions.
  1. +13 −0 nikola/plugins/shortcode/listing.plugin
  2. +46 −0 nikola/plugins/shortcode/listing.py
@@ -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

@@ -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

0 comments on commit 0f0670b

Please sign in to comment.