Skip to content

Instantly share code, notes, and snippets.

@FND
Created July 21, 2009 17:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save FND/151467 to your computer and use it in GitHub Desktop.
Save FND/151467 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
SAC
SAC ain't Cook (and he doesn't like it either)
Creates a TiddlyWiki document from a TiddlyWeb bag or recipe.
Usage:
sac.py <bag|recipe> <name>
"""
import sys
from tiddlyweb import control
from tiddlyweb.config import config
from tiddlyweb.model.tiddler import Tiddler
from tiddlyweb.model.bag import Bag
from tiddlyweb.model.recipe import Recipe
from tiddlyweb.store import Store
from tiddlyweb.serializer import Serializer
import tiddlywebwiki.plugin
env = { "tiddlyweb.config": config }
store = Store(config["server_store"][0], environ=env)
def get_tiddlers_from_bag(bag_name):
bag = store.get(Bag(bag_name))
return list(control.get_tiddlers_from_bag(bag))
def get_tiddlers_from_recipe(recipe_name):
recipe = store.get(Recipe(recipe_name))
return list(control.get_tiddlers_from_recipe(recipe))
def serialize_tiddlers(tiddlers, format):
serializer = Serializer(format, env)
# being a 'tmpbag' just means a deepcopy operation is skipped
tmpbag = Bag("tmp", tmpbag=True)
tmpbag.add_tiddlers(tiddlers)
return serializer.list_tiddlers(tmpbag)
def main(args):
tiddlywebwiki.plugin.init(config)
try:
type = args[1]
name = args[2]
if type == "bag":
tiddlers = get_tiddlers_from_bag(name)
elif type == "recipe":
tiddlers = get_tiddlers_from_recipe(name)
else:
print >> sys.stderr, "Error: argument %s not recognized" % type
return False
output = serialize_tiddlers(tiddlers, "tiddlywebwiki.serialization")
print output
return True
except IndexError:
print >> sys.stderr, "Error: missing arguments"
return False
if __name__ == "__main__":
status = not main(sys.argv)
sys.exit(status)
"""
Render TiddlyWiki syntax wikitext to HTML
using the WikklyText enginge.
"""
import wikklytext
import urllib
from tiddlyweb.web.util import encode_name
def render(tiddler, environ):
"""
Render TiddlyWiki wikitext in the provided
tiddler to HTML. The provided path helps
set paths in wikilinks correctly.
"""
server_prefix = environ.get('tiddlyweb.config',
{}).get('server_prefix', '')
if tiddler.recipe:
path = 'recipes/%s/tiddlers' % encode_name(tiddler.recipe)
elif tiddler.bag:
path = 'bags/%s/tiddlers' % encode_name(tiddler.bag)
else:
path = ''
html = wikitext_to_wikklyhtml('%s/' % server_prefix,
path, tiddler.text)
return unicode(html, 'utf-8')
def wikitext_to_wikklyhtml(base_url, path_url, wikitext):
"""
Turn a wikitext into HTML.
base_url: starting url for links in the wikitext (e.g. '/')
path_url: path from base to wikitext (e.g. 'recipes/foorecipe/tiddlers')
"""
def our_resolver(url_fragment, base_url, site_url):
"""
Turn url information for a wikiword into a link.
"""
if '://' in url_fragment or url_fragment.startswith('/'):
return url_fragment, True
return '%s%s' % (base_url, urllib.quote(url_fragment, safe='')), False
posthook = PostHook()
link_context = {
'$BASE_URL': '%s%s' % (base_url, path_url),
'$REFLOW': 0}
html, context = wikklytext.WikklyText_to_InnerHTML(
text=wikitext,
setvars=link_context,
encoding='utf-8',
safe_mode=True,
url_resolver=our_resolver,
tree_posthook=posthook.treehook)
return html
class PostHook(object):
"""
After we transform the wikitext into a
tree with need to link up the wiki words.
"""
def __init__(self):
# make map of wikiwords
self.wikiwords = InfiniteDict()
def treehook(self, rootnode, context):
"""
Turn wikiwords into links.
"""
from wikklytext.wikwords import wikiwordify
# add links to any wikiword
wikiwordify(rootnode, context, self.wikiwords)
class InfiniteDict(dict):
"""
Model a dictionary that returns
true for any key.
"""
def __getitem__(self, name):
return name
def has_key(self, name):
"""
This is an infiniate dict. It has all keys.
"""
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment