10000 re-added jekyll-rst · tisto/robotframework.github.com@9ed84c4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9ed84c4

Browse files
committed
re-added jekyll-rst
1 parent 8a423ba commit 9ed84c4

File tree

7 files changed

+324
-0
lines changed

7 files changed

+324
-0
lines changed

_plugins/jekyll-rst/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pyc

_plugins/jekyll-rst/LICENSE.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
Copyright (c) 2011 Greg Thornton, http://xdissent.com
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial por 10000 tions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.

_plugins/jekyll-rst/README.rst

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
Overview
2+
========
3+
4+
This plugin adds `ReStructuredText`_ support to `Jekyll`_ and `Octopress`_.
5+
It renders ReST in posts and pages, and provides a custom directive to
6+
support Octopress-compatible syntax highlighting.
7+
8+
Requirements
9+
============
10+
11+
* Jekyll *or* Octopress >= 2.0
12+
* Docutils
13+
* Pygments
14+
* `RbST`_
15+
16+
Installation
17+
============
18+
19+
1. Install Docutils and Pygments.
20+
21+
The most convenient way is to use virtualenv_burrito:
22+
23+
::
24+
25+
$ curl -s https://raw.github.com/brainsik/virtualenv-burrito/master/virtualenv-burrito.sh | bash
26+
$ source /Users/xdissent/.venvburrito/startup.sh
27+
$ mkvirtualenv jekyll-rst
28+
$ pip install docutils pygments
29+
30+
2. Install RbST.
31+
32+
If you use `bundler`_ with Octopress, add ``gem 'RbST'`` to
33+
your ``Gemfile`` in the ``development`` group, then run
34+
``bundle install``. Otherwise, ``gem install RbST``.
35+
36+
3. Install the plugin.
37+
38+
For Jekyll:
39+
40+
::
41+
42+
$ cd <jekyll-project-path>
43+
$ git submodule add https://github.com/xdissent/jekyll-rst.git _plugins/jekyll-rst
44+
45+
For Octopress:
46+
47+
::
48+
49+
$ cd <octopress-project-path>
50+
$ git submodule add https://github.com/xdissent/jekyll-rst.git plugins/jekyll-rst
51+
52+
4. Start blogging in ReStructuredText. Any file with the ``.rst`` extension
53+
will be parsed as ReST and rendered into HTML.
54+
55+
.. note:: Be sure to activate the ``jekyll-rst`` virtualenv before generating
56+
the site by issuing a ``workon jekyll-rst``. I suggest you follow `Harry
57+
Marr's advice`_ and create a ``.venv`` file that will automatically
58+
activate the ``jekyll-rst`` virtualenv when you ``cd`` into your project.
59+
60+
Source Code Highlighting
61+
========================
62+
63+
A ``code-block`` ReST directive is registered and aliased as ``sourcecode``.
64+
It adds syntax highlighting to code blocks in your documents::
65+
66+
.. code-block:: ruby
67+
68+
# Output "I love ReST"
69+
say = "I love ReST"
70+
puts say
71+
72+
Optional arguments exist to supply a caption, link, and link title::
73+
74+
.. code-block:: console
75+
:caption: Read Hacker News on a budget
76+
:url: http://news.ycombinator.com
77+
:title: Hacker News
78+
79+
$ curl http://news.ycombinator.com | less
80+
81+
Octopress already includes style sheets for syntax highlighting, but you'll
82+
need to generate one yourself if using Jekyll::
83+
84+
$ pygmentize -S default -f html > css/pygments.css
85+
86+
Octopress Tips
87+
==============
88+
89+
* Use ``.. more`` in your ReST documents to indicate where Octopress's
90+
``excerpt`` tag should split your content for summary views.
91+
92+
.. _ReStructuredText: http://docutils.sourceforge.net/rst.html
93+
.. _Jekyll: http://jekyllrb.com/
94+
.. _Octopress: http://octopress.com/
95+
.. _RbST: http://rubygems.org/gems/RbST
96+
.. _bundler: http://gembundler.com/
97+
.. _Harry Marr's advice: http://hmarr.com/2010/jan/19/making-virtualenv-play-nice-with-git/

_plugins/jekyll-rst/converter.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require 'rbst'
2+
3+
module Jekyll
4+
class RestConverter < Converter
5+
safe true
6+
7+
priority :low
8+
9+
def matches(ext)
10+
ext =~ /rst/i
11+
end
12+
13+
def output_ext(ext)
14+
".html"
15+
end
16+
17+
def convert(content)
18+
RbST.executables = {:html => "#{File.expand_path(File.dirname(__FILE__))}/rst2html.py"}
19+
RbST.new(content).to_html(:part => :fragment, :initial_header_level => 2)
20+
end
21+
end
22+
23+
module Filters
24+
def restify(input)
25+
site = @context.registers[:site]
26+
converter = site.getConverterImpl(Jekyll::RestConverter)
27+
converter.convert(input)
28+
end
29+
end
30+
end

_plugins/jekyll-rst/directives.py

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Define a new directive `code-block` (aliased as `sourcecode`) that uses the
2+
# `pygments` source highlighter to render code in color.
3+
#
4+
# Incorporates code from the `Pygments`_ documentation for `Using Pygments in
5+
# ReST documents`_ and `Octopress`_.
6+
#
7+
# .. _Pygments: http://pygments.org/
8+
# .. _Using Pygments in ReST documents: http://pygments.org/docs/rstdirective/
9+
# .. _Octopress: http://octopress.org/
10+
11+
import re
12+
import os
13+
import md5
14+
import __main__
15+
16+
# Absolute path to pygments cache dir
17+
PYGMENTS_CACHE_DIR = os.path.abspath(os.path.join(os.path.dirname(__main__.__file__), '../../.pygments-cache'))
18+
19+
# Ensure cache dir exists
20+
if not os.path.exists(PYGMENTS_CACHE_DIR):
21+
os.makedirs(PYGMENTS_CACHE_DIR)
22+
23+
from pygments.formatters import HtmlFormatter
24+
25+
from docutils import nodes
26+
from docutils.parsers.rst import directives, Directive
27+
28+
from pygments import highlight
29+
from pygments.lexers import get_lexer_by_name, TextLexer
30+
31+
class Pygments(Directive):
32+
""" Source code syntax hightlighting.
33+
"""
34+
required_arguments = 1
35+
optional_arguments = 0
36+
final_argument_whitespace = True
37+
string_opts = ['title', 'url', 'caption']
38+
option_spec = dict([(key, directives.unchanged) for key in string_opts])
39+
has_content = True
40+
41+
def run(self):
42+
self.assert_has_content()
43+
try:
44+
lexer_name = self.arguments[0]
45+
lexer = get_lexer_by_name(lexer_name)
46+
except ValueError:
47+
# no lexer found - use the text one instead of an exception
48+
lexer_name = 'text'
49+
lexer = TextLexer()
50+
formatter = HtmlFormatter()
51+
52+
# Construct cache filename
53+
cache_file = None
54+
content_text = u'\n'.join(self.content)
55+
cache_file_name = '%s-%s.html' % (lexer_name, md5.new(content_text).hexdigest())
56+
cached_path = os.path.join(PYGMENTS_CACHE_DIR, cache_file_name)
57+
58+
# Look for cached version, otherwise parse
59+
if os.path.exists(cached_path):
60+
cache_file = open(cached_path, 'r')
61+
parsed = cache_file.read()
62+
else:
63+
parsed = highlight(content_text, lexer, formatter)
64+
65+
# Strip pre tag and everything outside it
66+
pres = re.compile("<pre>(.+)<\/pre>", re.S)
67+
stripped = pres.search(parsed).group(1)
68+
69+
# Create tabular code with line numbers
70+
table = '<div class="highlight"><table><tr><td class="gutter"><pre class="line-numbers">'
71+
lined = ''
72+
for idx, line in enumerate(stripped.splitlines(True)):
73+
table += '<span class="line-number">%d</span>\n' % (idx + 1)
74+
lined += '<span class="line">%s</span>' % line
75+
table += '</pre></td><td class="code"><pre><code class="%s">%s</code></pre></td></tr></table></div>' % (lexer_name, lined)
76+
77+
# Add wrapper with optional caption and link
78+
code = '<figure class="code">'
79+
if self.options:
80+
caption = ('<span>%s</span>' % self.options['caption']) if 'caption' in self.options else ''
81+
title = self.options['title'] if 'title' in self.options else 'link'
82+
link = ('<a href="%s">%s</a>' % (self.options['url'], title)) if 'url' in self.options else ''
83+
84+
if caption or link:
85+
code += '<figcaption>%s %s</figcaption>' % (caption, link)
86+
code += '%s</figure>' % table
87+
88+
# Write cache
89+
if cache_file is None:
90+
cache_file = open(cached_path, 'w')
91+
cache_file.write(parsed)
92+
cache_file.close()
93+
94+
return [nodes.raw('', code, format='html')]
95+
96+
directives.register_directive('code-block', Pygments)
97+
directives.register_directive('sourcecode', Pygments)

_plugins/jekyll-rst/rst2html.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env python
2+
3+
# :Author: David Goodger, the Pygments team, Guenter Milde
4+
# :Date: $Date: $
5+
# :Copyright: This module has been placed in the public domain.
6+
7+
# This is a merge of the `Docutils`_ `rst2html` front end with an extension
8+
# suggestion taken from the `Pygments`_ documentation, reworked specifically
9+
# for `Octopress`_.
10+
#
11+
# .. _Pygments: http://pygments.org/
12+
# .. _Docutils: http://docutils.sourceforge.net/
13+
# .. _Octopress: http://octopress.org/
14+
15+
"""
16+
A front end to docutils, producing HTML with syntax colouring using pygments
17+
"""
18+
19+
try:
20+
import locale
21+
locale.setlocale(locale.LC_ALL, '')
22+
except:
23+
pass
24+
25+
from transform import transform
26+
from docutils.writers.html4css1 import Writer
27+
from docutils.core import default_description
28+
from directives import Pygments
29+
30+
description = ('Generates (X)HTML documents from standalone reStructuredText '
31+
'sources. Uses `pygments` to colorize the content of'
32+
'"code-block" directives. Needs an adapted stylesheet'
33+
+ default_description)
34+
35+
def main():
36+
return transform(writer=Writer(), part='html_body')
37+
38+
if __name__ == '__main__':
39+
print(main())
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import sys
2+
from docutils.core import publish_parts
3+
from optparse import OptionParser
4+
from docutils.frontend import OptionParser as DocutilsOptionParser
5+
from docutils.parsers.rst import Parser
6+
7+
def transform(writer=None, part=None):
8+
p = OptionParser(add_help_option=False)
9+
10+
# Collect all the command line options
11+
docutils_parser = DocutilsOptionParser(components=(writer, Parser()))
12+
for group in docutils_parser.option_groups:
13+
p.add_option_group(group.title, None).add_options(group.option_list)
14+
15+
p.add_option('--part', default=part)
16+
17+
opts, args = p.parse_args()
18+
19+
settings = dict({
20+
'file_insertion_enabled': False,
21+
'raw_enabled': False,
22+
}, **opts.__dict__)
23+
24+
if len(args) == 1:
25+
try:
26+
content = open(args[0], 'r').read()
27+
except IOError:
28+
content = args[0]
29+
else:
30+
content = sys.stdin.read()
31+
32+
parts = publish_parts(
33+
source=content,
34+
settings_overrides=settings,
35+
writer=writer,
36+
)
37+
38+
if opts.part in parts:
39+
return parts[opts.part]
40+
return ''

0 commit comments

Comments
 (0)
0