Anonymous avatar Anonymous committed 04e410b

added directive and block syntax, with pygments highlight plugin to demo

Comments (0)

Files changed (11)

 want.  You just can't go claiming you wrote it, nor can you sue me if
 it blows your system to smithereens (trust me, it probably won't).
 
-Other parts of nom (just code used by plugins at this time) have their
-own licenses.  If you redistribute the plugins, you need to obey their
-licenses too.  I've only picked free licenses that have no "copyleft"
-requirements that obligate you to distribute source (of course, given
-that it's python, you're probably distributing it as source anyway!)
+Other parts of nom have their own licenses.  If you redistribute the
+plugins, you need to obey their licenses too.  I've only picked free
+licenses that have no "copyleft" requirements that obligate you to
+distribute source (of course, given that it's python, you're probably
+distributing it as source anyway!)
 
 END UNOFFICIAL INTERPRETATION, BEGIN LEGALESE BOILERPLATE:
 
 
 
 
+PyRSS2Gen (in feed_generator.py)
+================================
+
+Copyright (c) 2003, Dalke Scientific Software, LLC
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+  * Redistributions of source code must retain the above copyright
+    notice, this list of conditions and the following disclaimer.
+
+  * Redistributions in binary form must reproduce the above copyright
+    notice, this list of conditions and the following disclaimer in
+    the documentation and/or other materials provided with the
+    distribution.
+
+  * Neither the name of the Dalke Scientific Softare, LLC, Andrew
+    Dalke, nor the names of its contributors may be used to endorse or
+    promote products derived from this software without specific prior
+    written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+
+
+
 Textile (in plugins/textile)
 ============================
 
             logging.warn("couldn't translate infile to outfile: %s => %s" % (infile, outfile))
             outfile = os.path.join(outdir, newfile)
 
+        if env.get('output-basename'):
+            # override chosen basename with provided one
+            outfile = os.path.join(os.path.dirname(outfile), env['output-basename'])
+
         logging.debug("get_output_filename: " + outfile)
+
         # not used further here, but needs to be recorded for tracker
         env['output-filename'] = outfile
-
         return outfile
 
     def process(self, data, env):

nom/plugins/__init__.py

 
 __path__ = nom.config.plugin_dirs + __path__
 
+# XXX look into this later
+# from pkgutil import extend_path
+# __path__ = extend_path(__path__, __name__)
 
 
 

nom/plugins/block.py

+from nom.handler import Filter, register_handler
+
+import re
+import logging
+
+
+class BlockFilter(Filter):
+    name = 'block'
+
+    block_re = re.compile(r'''
+
+    \[\[=
+    (?P<name> [-\w]+ )
+    (?: \( (?P<argstr>  .*?) \) )?
+    (?: \# (?P<delim> [-/\w]+) )?
+
+    :
+
+    (?P<content> .*? )
+    
+    (?(delim)(?P=delim))
+    \]\]
+
+    ''', re.S | re.X)
+
+    # This has higher priority than anything, including directives!
+    # XXX consider just moving this to BEFORE_FILTER instead.
+    priority      = 2000
+
+    @classmethod
+    def accept(cls, env):
+        # We need to accept pretty much any content-type
+        return True
+
+    def sub_block(self, match):
+        # expand the groupdict, the use of a regex is just an
+        # implementation detail
+        return substitute_block(self.env, **match.groupdict())
+
+    def _process_string(self, string, env):
+        self.env = env
+        string = self.block_re.sub(self.sub_block, string)
+
+        return (string, env)
+
+register_handler(BlockFilter)
+
+from nom.task import STAGE_AFTER_FILTER
+class BlockSubber(Filter):
+    name = 'blocksubber'
+    handles_types = ['text/html']
+    stages = [STAGE_AFTER_FILTER]
+
+    def _process_string(self, string, env):
+        string = re.sub(r'\[\[@block:(\w+)\]\]', lambda m: env['blocks'][m.group(1)], string)
+        return (string, env)
+    
+register_handler(BlockSubber)
+_registry = {}
+
+def register_block(name, callback, protect=False, autostrip=True):
+    _registry[name] = (callback, protect, autostrip)
+
+_block_counter = 0
+def substitute_block(env, name, content, argstr=None, delim=None):
+    (callback, protect, autostrip) = _registry[name]
+
+    if autostrip:
+        if name:    name = name.strip()
+        if content: content = content.strip()
+        if argstr:  argstr = argstr.strip()
+        if delim:   delim = delim.strip()
+        
+    result = callback(env, name, content, argstr, delim)
+
+    if protect:
+        # protect from further output munging by inserting a fixup
+        # token that is substituted after everything else runs.  Uses
+        # globals, so it's obviously not threadsafe.
+        global _block_counter
+        _block_counter += 1
+        env.setdefault('blocks', {})[str(_block_counter)] = result
+        result = '[[@block:%s]]' % (_block_counter,)
+
+    return result
+    
+    
+
+    

nom/plugins/directives.py

+from nom.handler import Filter, register_handler
+
+import re
+import logging
+
+class DirectivesFilter(Filter):
+    name = 'directives'
+
+    directive_re = re.compile(r'\[\[!(.*?)\]\]', re.S)
+
+    # High priority, should run first.  XXX TODO should probably move
+    # this to pre/post filter stages.
+    priority      = 1000
+
+    # XXX probably need to handle all types and make
+    # register_directive possibly type-aware
+    handles_types = ['text/html']
+
+    def sub_directive(self, match):
+        text = match.group(1)
+        # Hardwired informational directives for now
+        m = re.search(r'^(title|author|blurb):\s*(.*)', text)
+        if m:
+            key = m.group(1)
+            val = m.group(2)
+            self.env[key] = val
+
+        # The default is to simply strip the directive
+        return ''
+        
+
+    def _process_string(self, string, env):
+        # stash env, we'll need it again
+        self.env = env
+        string = self.directive_re.sub(self.sub_directive, string)
+
+        return (string, env)
+
+register_handler(DirectivesFilter)
+
+# TODO:
+#
+#    * register_directive allowing plugins to contribute directives
+#
+#    * [[@module:directive]] and [[%module:directive]]
+#         early and late directives, namespaced to a module.
+#         These might not even need registering.
+

nom/plugins/highlight/__init__.py

+from nom.handler import Filter, register_handler
+from nom.plugins.block import register_block
+
+from pygments import highlight
+from pygments.lexers import (get_lexer_by_name, get_lexer_for_mimetype,
+                             guess_lexer, guess_lexer_for_filename)
+
+from pygments.formatters import HtmlFormatter
+
+from os.path import basename, splitext
+
+class CodeHighlighter(Filter):
+    name = 'codehl'
+
+    priority      = 100
+    handles_types = ['text/x-python']
+
+    def _process_string(self, string, env):
+        env['content-type'] = 'text/html'
+
+        # icky hack to turn foo.py to foo_py.html
+        infile = env['input-filename']
+        (stem, ext) = splitext(infile)
+        env['output-basename'] = "%s_%s.html" % (basename(stem), ext[1:])
+
+        lexer = guess_lexer_for_filename(infile, stripall=True)
+        formatter = HtmlFormatter(linenos=False, cssclass="hl")
+        string = highlight(string, lexer, formatter)
+        return (string, env)
+                
+# NOT registered by default (it's potentially dangerous)
+# register_handler(CodeHighlighter)
+
+
+def highlight_block(env, name, content, argstr=None, delim=None):
+    if argstr:
+        lexer = get_lexer_by_name(argstr, stripall=True)
+    else:
+        lexer = guess_lexer(content)
+        
+    formatter = HtmlFormatter(linenos=False, cssclass="hl")
+    return highlight(content, lexer, formatter)
+
+register_block('code', highlight_block, protect=True)
+
+
+
+
+
 import sys
 import os
 
-from nom.task import Driver
+from nom.session import Session
 import nom.config
-from nom.plugins import markdown, textile, rst, wikilinks, simpletemplate
+from nom.plugins import (
+    block, directives, highlight,
+    markdown, textile, rss, rst, simpletemplate, wikilinks
+    )
 
 import logging
 
 options.sourcedir and options.outdir or die_with_usage()
 
 
-d = Driver(sourcedir = options.sourcedir, outdir = options.outdir)
-d.run()
+s = Session(sourcedir = options.sourcedir, outdir = options.outdir)
+s.run()

t/src/_templates/__default__.tpl

 <html>
-<title>${title}</title>
+<head>
+  <title>${title}</title>
+  <link rel="stylesheet" href="/nomtest/highlight.css" media="screen, print">
+  ${extrahead}
+</head>
 <body>
+${header}
 $__content__
+${footer}
 </body>
 </html>

t/src/big/long/nested/path/to/here.html

+[[!title: Link Test]]
 
 This is a [[wikilink]] with no spaces
 
 
 This is a [[  wiki link with even more spaces ]]
 
-This is a [[Wiki Link With a Title|titlelink]]
+This is a [[titlelink|Wiki Link With a Title]]
 
-This is [[!a directive]] and is NOT detected as a link
+A directive [[!like this directive]] is removed and not detected as a link
 
-This is [[Titled URL|http://foo.bar.com?blah=woof#quux]]
+This is [[http://foo.bar.com?blah=woof#quux|Titled Url]]
+
+Escaping: \[\[All brackets\]\] can be escaped
+
+But [\[only one]] pair needs to [[be interrupted]\]
+
+There is ![[no prefix escape]] yet like ikiwiki

t/src/code/codeblocks.mdwn

+[[=code:
+#!/usr/bin/python
+
+from pygments import highlight
+from pygments.lexers import get_lexer_by_name
+from pygments.formatters import HtmlFormatter
+
+lexer = get_lexer_by_name("python", stripall=True)
+formatter = HtmlFormatter(linenos=True, cssclass="source")
+result = highlight(code, lexer, formatter)
+
+]]
+
+[[=code:
+#!/bin/sh
+
+export FOO=$(bar baz)
+# comment
+
+exec foo --bar >/dev/null
+
+]]

t/src/highlight.css

+
+.hl  { background: #f8f8f8; }
+.hl .c { color: #408080; font-style: italic } /* Comment */
+.hl .err { border: 1px solid #FF0000 } /* Error */
+.hl .k { color: #008000; font-weight: bold } /* Keyword */
+.hl .o { color: #666666 } /* Operator */
+.hl .cm { color: #408080; font-style: italic } /* Comment.Multiline */
+.hl .cp { color: #BC7A00 } /* Comment.Preproc */
+.hl .c1 { color: #408080; font-style: italic } /* Comment.Single */
+.hl .cs { color: #408080; font-style: italic } /* Comment.Special */
+.hl .gd { color: #A00000 } /* Generic.Deleted */
+.hl .ge { font-style: italic } /* Generic.Emph */
+.hl .gr { color: #FF0000 } /* Generic.Error */
+.hl .gh { color: #000080; font-weight: bold } /* Generic.Heading */
+.hl .gi { color: #00A000 } /* Generic.Inserted */
+.hl .go { color: #808080 } /* Generic.Output */
+.hl .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
+.hl .gs { font-weight: bold } /* Generic.Strong */
+.hl .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
+.hl .gt { color: #0040D0 } /* Generic.Traceback */
+.hl .kc { color: #008000; font-weight: bold } /* Keyword.Constant */
+.hl .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
+.hl .kp { color: #008000 } /* Keyword.Pseudo */
+.hl .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
+.hl .kt { color: #B00040 } /* Keyword.Type */
+.hl .m { color: #666666 } /* Literal.Number */
+.hl .s { color: #BA2121 } /* Literal.String */
+.hl .na { color: #7D9029 } /* Name.Attribute */
+.hl .nb { color: #008000 } /* Name.Builtin */
+.hl .nc { color: #0000FF; font-weight: bold } /* Name.Class */
+.hl .no { color: #880000 } /* Name.Constant */
+.hl .nd { color: #AA22FF } /* Name.Decorator */
+.hl .ni { color: #999999; font-weight: bold } /* Name.Entity */
+.hl .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
+.hl .nf { color: #0000FF } /* Name.Function */
+.hl .nl { color: #A0A000 } /* Name.Label */
+.hl .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
+.hl .nt { color: #008000; font-weight: bold } /* Name.Tag */
+.hl .nv { color: #19177C } /* Name.Variable */
+.hl .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
+.hl .w { color: #bbbbbb } /* Text.Whitespace */
+.hl .mf { color: #666666 } /* Literal.Number.Float */
+.hl .mh { color: #666666 } /* Literal.Number.Hex */
+.hl .mi { color: #666666 } /* Literal.Number.Integer */
+.hl .mo { color: #666666 } /* Literal.Number.Oct */
+.hl .sb { color: #BA2121 } /* Literal.String.Backtick */
+.hl .sc { color: #BA2121 } /* Literal.String.Char */
+.hl .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */
+.hl .s2 { color: #BA2121 } /* Literal.String.Double */
+.hl .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
+.hl .sh { color: #BA2121 } /* Literal.String.Heredoc */
+.hl .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
+.hl .sx { color: #008000 } /* Literal.String.Other */
+.hl .sr { color: #BB6688 } /* Literal.String.Regex */
+.hl .s1 { color: #BA2121 } /* Literal.String.Single */
+.hl .ss { color: #19177C } /* Literal.String.Symbol */
+.hl .bp { color: #008000 } /* Name.Builtin.Pseudo */
+.hl .vc { color: #19177C } /* Name.Variable.Class */
+.hl .vg { color: #19177C } /* Name.Variable.Global */
+.hl .vi { color: #19177C } /* Name.Variable.Instance */
+.hl .il { color: #666666 } /* Literal.Number.Integer.Long */
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.