Skip to content

Commit 06286f9

Browse files
committedMay 23, 2015
fix #1710 -- add a rst2html command
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
1 parent 0fdb917 commit 06286f9

File tree

5 files changed

+124
-30
lines changed

5 files changed

+124
-30
lines changed
 

‎CHANGES.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ New in master
44
Features
55
--------
66

7+
* New `nikola rst2html` command (Issue #1710)
8+
* New `nikola status` command (Issue #1740)
79
* Support [code] in wordpress importers (Issue #1186)
8-
* New `nikola status` command
910
* Cleaner formatting of HTML output
1011
* Allowing category hierarchies via new option CATEGORY_ALLOW_HIERARCHIES
1112
(Issue #1520)
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[Core]
2+
Name = rst2html
3+
Module = rst2html
4+
5+
[Documentation]
6+
Author = Chris Warrick
7+
Version = 1.0
8+
Website = http://getnikola.com
9+
Description = Compile reStructuredText to HTML using the Nikola architecture
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# -*- coding: utf-8 -*-
2+
3+
# Copyright © 2015 Chris Warrick and others.
4+
5+
# Permission is hereby granted, free of charge, to any
6+
# person obtaining a copy of this software and associated
7+
# documentation files (the "Software"), to deal in the
8+
# Software without restriction, including without limitation
9+
# the rights to use, copy, modify, merge, publish,
10+
# distribute, sublicense, and/or sell copies of the
11+
# Software, and to permit persons to whom the Software is
12+
# furnished to do so, subject to the following conditions:
13+
#
14+
# The above copyright notice and this permission notice
15+
# shall be included in all copies or substantial portions of
16+
# the Software.
17+
#
18+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
19+
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
20+
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
21+
# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
22+
# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23+
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24+
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25+
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26+
27+
from __future__ import unicode_literals, print_function
28+
29+
import io
30+
import lxml.html
31+
from pkg_resources import resource_filename
32+
from mako.template import Template
33+
from nikola.plugin_categories import Command
34+
35+
class CommandRst2Html(Command):
36+
"""Compile reStructuredText to HTML, using Nikola architecture."""
37+
38+
name = "rst2html"
39+
doc_usage = "infile"
40+
doc_purpose = "compile reStructuredText to HTML files"
41+
42+
def _execute(self, options, args):
43+
"""Compile reStructuredText to standalone HTML files."""
44+
compiler = self.site.plugin_manager.getPluginByName('rest', 'PageCompiler').plugin_object
45+
if len(args) != 1:
46+
print("This command takes only one argument (input file name).")
47+
return 2
48+
source = args[0]
49+
with io.open(source, "r", encoding="utf8") as in_file:
50+
data = in_file.read()
51+
output, error_level, deps = compiler.compile_html_string(data, source, True)
52+
53+
rstcss_path = resource_filename('nikola', 'data/themes/base/assets/css/rst.css')
54+
with io.open(rstcss_path, "r", encoding="utf8") as fh:
55+
rstcss = fh.read()
56+
57+
template_path = resource_filename('nikola', 'plugins/command/rst2html/rst2html.tmpl')
58+
template = Template(filename=template_path)
59+
template_output = template.render(rstcss=rstcss, output=output)
60+
parser = lxml.html.HTMLParser(remove_blank_text=True)
61+
doc = lxml.html.document_fromstring(template_output, parser)
62+
html = b'<!DOCTYPE html>\n' + lxml.html.tostring(doc, encoding='utf8', method='html', pretty_print=True)
63+
print(html)
64+
if error_level < 3:
65+
return 0
66+
else:
67+
return 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<style class="text/css">
6+
${rstcss}
7+
</style>
8+
</head>
9+
10+
<body>
11+
${output}
12+
</body>
13+
</html>

‎nikola/plugins/compile/rest/__init__.py

+33-29
Original file line numberDiff line numberDiff line change
@@ -64,43 +64,47 @@ def register_extra_dependencies(self, post):
6464
"""Adds dependency to post object to check .dep file."""
6565
post.add_dependency(lambda: self._read_extra_deps(post), 'fragment')
6666

67-
def compile_html(self, source, dest, is_two_file=True):
68-
"""Compile reSt into HTML."""
67+
def compile_html_string(self, data, source_path=None, is_two_file=True):
68+
"""Compile reSt into HTML strings."""
69+
add_ln = 0
70+
if not is_two_file:
71+
spl = re.split('(\n\n|\r\n\r\n)', data, maxsplit=1)
72+
data = spl[-1]
73+
if len(spl) != 1:
74+
# If errors occur, this will be added to the line
75+
# number reported by docutils so the line number
76+
# matches the actual line number (off by 7 with default
77+
# metadata, could be more or less depending on the post
78+
# author).
79+
add_ln = len(spl[0].splitlines()) + 1
80+
81+
default_template_path = os.path.join(os.path.dirname(__file__), 'template.txt')
82+
output, error_level, deps = rst2html(
83+
data, settings_overrides={
84+
'initial_header_level': 1,
85+
'record_dependencies': True,
86+
'stylesheet_path': None,
87+
'link_stylesheet': True,
88+
'syntax_highlight': 'short',
89+
'math_output': 'mathjax',
90+
'template': default_template_path,
91+
}, logger=self.logger, source_path=source_path, l_add_ln=add_ln, transforms=self.site.rst_transforms)
92+
if not isinstance(output, unicode_str):
93+
# To prevent some weird bugs here or there.
94+
# Original issue: empty files. `output` became a bytestring.
95+
output = output.decode('utf-8')
96+
return output, error_level, deps
6997

98+
def compile_html(self, source, dest, is_two_file=True):
99+
"""Compile reSt into HTML files."""
70100
if not has_docutils:
71101
req_missing(['docutils'], 'build this site (compile reStructuredText)')
72102
makedirs(os.path.dirname(dest))
73103
error_level = 100
74104
with io.open(dest, "w+", encoding="utf8") as out_file:
75105
with io.open(source, "r", encoding="utf8") as in_file:
76106
data = in_file.read()
77-
add_ln = 0
78-
if not is_two_file:
79-
spl = re.split('(\n\n|\r\n\r\n)', data, maxsplit=1)
80-
data = spl[-1]
81-
if len(spl) != 1:
82-
# If errors occur, this will be added to the line
83-
# number reported by docutils so the line number
84-
# matches the actual line number (off by 7 with default
85-
# metadata, could be more or less depending on the post
86-
# author).
87-
add_ln = len(spl[0].splitlines()) + 1
88-
89-
default_template_path = os.path.join(os.path.dirname(__file__), 'template.txt')
90-
output, error_level, deps = rst2html(
91-
data, settings_overrides={
92-
'initial_header_level': 1,
93-
'record_dependencies': True,
94-
'stylesheet_path': None,
95-
'link_stylesheet': True,
96-
'syntax_highlight': 'short',
97-
'math_output': 'mathjax',
98-
'template': default_template_path,
99-
}, logger=self.logger, source_path=source, l_add_ln=add_ln, transforms=self.site.rst_transforms)
100-
if not isinstance(output, unicode_str):
101-
# To prevent some weird bugs here or there.
102-
# Original issue: empty files. `output` became a bytestring.
103-
output = output.decode('utf-8')
107+
output, error_level, deps = self.compile_html_string(data, source, is_two_file)
104108
out_file.write(output)
105109
deps_path = dest + '.dep'
106110
if deps.list:

0 commit comments

Comments
 (0)
Please sign in to comment.