Skip to content

Instantly share code, notes, and snippets.

@ianlewis
Last active December 14, 2016 00:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianlewis/978201 to your computer and use it in GitHub Desktop.
Save ianlewis/978201 to your computer and use it in GitHub Desktop.
Redmine restructured text formatter.
u"""
sudo pip install docutils pygments # システム Python に docutils インストール
rvm use 1.8.7
gem install RbST
/var/www/vhosts/redmine/script/plugin install git://github.com/alphabetum/redmine_restructuredtext_formatter.git
RbST はなんと! 日本語に対応しません! orz (幸いなことに、 docutils の部分は Python で書かれている)
cd /var/www/.rvm/gems/ruby-1.8.7-p334/gems/RbST-0.1.3
wget -O aaa.diff --no-check-certificate https://github.com/IanLewis/rbst/compare/1cfc990...e957653.diff
patch -p 1 < aaa.diff
Wiki リンク、コードハイライトに対応するため、Python スクリプトを修正
vim /var/www/.rvm/gems/ruby-1.8.7-p334/gems/RbST-0.1.3/lib/rst2parts/rst2html.py
"""
#!/usr/bin/env python
try:
import locale
locale.setlocale(locale.LC_ALL, '')
except:
pass
import sys
from docutils import nodes
from docutils.writers import html4css1
from docutils.core import publish_parts
from docutils.parsers.rst import directives
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import get_lexer_by_name, TextLexer
from transform import transform
from docutils.writers.html4css1 import Writer
VARIANTS = {}
def pygments_directive(name, arguments, options, content, lineno,
content_offset, block_text, state, state_machine):
try:
lexer = get_lexer_by_name(arguments[0])
except (ValueError, IndexError):
# no lexer found - use the text one instead of an exception
lexer = TextLexer()
parsed = highlight(u"\n".join(content), lexer, HtmlFormatter(noclasses=True))
return [nodes.raw("", parsed, format="html")]
pygments_directive.arguments = (0, 1, False)
pygments_directive.content = 1
pygments_directive.options = dict([(key, directives.flag) for key in VARIANTS])
directives.register_directive("sourcecode", pygments_directive)
directives.register_directive("code-block", pygments_directive)
class WikiWriter(Writer):
def __init__(self, *args, **kwargs):
Writer.__init__(self, *args, **kwargs)
self.unknown_reference_resolvers = [self.wiki_resolver]
self.nodes = []
def wiki_resolver(self, node):
"""
Normally an unknown reference would be an error in an reST document.
However, this is how new documents are created in the wiki. This
passes on unknown references to eventually be handled by
MoinMoin.
"""
if hasattr(node, 'indirect_reference_name'):
node['refuri'] = node.indirect_reference_name
elif (len(node['ids']) != 0):text
# If the node has an id then it's probably an internal link. Let
# docutils generate an error.
return False
elif node.hasattr('name'):
node['refuri'] = node['name']
else:
node['refuri'] = node['refname']
del node['refname']
if hasattr(self, 'nodes'):
node.resolved = 1
self.nodes.append(node)
return True
else:
return False
wiki_resolver.priority = 1
def main():
return transform(writer=WikiWriter(), part='html_body')
if __name__ == '__main__':
try:
output = main()
if output:
sys.stdout.write(output)
except Exception, e:
sys.stdout.write('<strong style="color:red">Error Parsing ReSt: %r</strong>' % e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment