-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ee96d32
Showing
5 changed files
with
109 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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.pyc | ||
/*.egg-info/ |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from setuptools import find_packages, setup | ||
|
||
setup(name='TracBlockdiagPlugin', | ||
version='0.1.0', | ||
packages=find_packages(exclude=['*.tests*']), | ||
install_requires=['blockdiag>=0.8.1'], | ||
entry_points = """ | ||
[trac.plugins] | ||
tracblockdiag = tracblockdiag.plugin | ||
""" | ||
) |
Empty file.
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import os | ||
|
||
from blockdiag.diagparser import parse, tokenize | ||
from blockdiag.builder import ScreenNodeBuilder | ||
from blockdiag.DiagramDraw import DiagramDraw | ||
|
||
try: | ||
from cStringIO import StringIO | ||
except ImportError: | ||
from StringIO import StringIO | ||
|
||
|
||
def detectfont(prefer=None): | ||
prefer = prefer or [] | ||
fonts = prefer + \ | ||
['c:/windows/fonts/VL-Gothic-Regular.ttf', # for Windows | ||
'c:/windows/fonts/msmincho.ttf', # for Windows | ||
'/usr/share/fonts/truetype/ipafont/ipagp.ttf', # for Debian | ||
'/usr/local/share/font-ipa/ipagp.otf', # for FreeBSD | ||
'/System/Library/Fonts/AppleGothic.ttf'] # for MaxOS | ||
for font in fonts: | ||
if font and os.path.isfile(font): | ||
return font | ||
|
||
|
||
def get_diag(text, fmt, font=None, antialias=True, nodoctype=False): | ||
if not isinstance(fmt, basestring): | ||
return None | ||
fmt = fmt.upper() | ||
if fmt not in ('SVG', 'PNG'): | ||
return None | ||
|
||
tree = parse(tokenize(text)) | ||
diagram = ScreenNodeBuilder.build(tree) | ||
fp = None if fmt == 'SVG' else StringIO() | ||
drawer = DiagramDraw(fmt, diagram, fp, font=font, | ||
antialias=antialias, nodoctype=nodoctype) | ||
drawer.draw() | ||
if fmt == 'SVG': | ||
return drawer.save() | ||
if fmt == 'PNG': | ||
drawer.save() | ||
diag = fp.getvalue() | ||
try: | ||
fp.close() | ||
except: | ||
pass | ||
return diag |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import re | ||
from base64 import b64encode, b64decode | ||
|
||
from trac.core import * | ||
from trac.util.html import html | ||
from trac.web import IRequestHandler | ||
from trac.wiki import IWikiMacroProvider | ||
from trac.wiki.formatter import system_message | ||
|
||
from .diag import detectfont, get_diag | ||
|
||
macro_defs = {'blockdiag': 'description'} | ||
content_types = {'png': 'image/png', | ||
'svg': 'image/svg+xml'} | ||
|
||
|
||
class BlockdiagRenderer(Component): | ||
implements(IWikiMacroProvider, IRequestHandler) | ||
|
||
def __init__(self): | ||
self.font = detectfont() | ||
self.url = re.compile(r'/blockdiag/([a-z]+)/(png|svg)/(.+)') | ||
self.src = 'blockdiag/%(type)s/%(fmt)s/%(data)s' | ||
|
||
def get_macros(self): | ||
return macro_defs.keys() | ||
|
||
def get_macro_description(self, name): | ||
return macro_defs.get(name, '') | ||
|
||
def expand_macro(self, formatter, name, content, args=None): | ||
args = args or {} | ||
params = {'type': name[:-4], 'data': b64encode(content), | ||
'fmt': args.pop('type', 'png')} | ||
args['src'] = formatter.req.href(self.src % params) | ||
return html.img(**args) | ||
|
||
def match_request(self, req): | ||
return bool(self.url.match(req.path_info)) | ||
|
||
def process_request(self, req): | ||
type_, fmt, data = self.url.match(req.path_info).groups() | ||
diag = get_diag(b64decode(data), fmt, self.font) | ||
req.send(diag, content_types.get(fmt.lower(), ''), status=200) |