aboutsummaryrefslogtreecommitdiffstats
path: root/nikola/plugins/compile_rest
diff options
context:
space:
mode:
Diffstat (limited to 'nikola/plugins/compile_rest')
-rw-r--r--nikola/plugins/compile_rest/__init__.py34
-rw-r--r--nikola/plugins/compile_rest/pygments_code_block_directive.py38
-rw-r--r--nikola/plugins/compile_rest/slides.py89
-rw-r--r--nikola/plugins/compile_rest/youtube.py24
4 files changed, 170 insertions, 15 deletions
diff --git a/nikola/plugins/compile_rest/__init__.py b/nikola/plugins/compile_rest/__init__.py
index 0a25a06..0e677e1 100644
--- a/nikola/plugins/compile_rest/__init__.py
+++ b/nikola/plugins/compile_rest/__init__.py
@@ -1,3 +1,28 @@
+# Copyright (c) 2012 Roberto Alsina y otros.
+
+# Permission is hereby granted, free of charge, to any
+# person obtaining a copy of this software and associated
+# documentation files (the "Software"), to deal in the
+# Software without restriction, including without limitation
+# the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the
+# Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice
+# shall be included in all copies or substantial portions of
+# the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
+# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
+# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
+# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+from __future__ import unicode_literals
import codecs
import os
@@ -5,17 +30,16 @@ import docutils.core
import docutils.io
from docutils.parsers.rst import directives
-from pygments_code_block_directive import (
+from .pygments_code_block_directive import (
code_block_directive,
listings_directive)
directives.register_directive('code-block', code_block_directive)
directives.register_directive('listing', listings_directive)
-import pygments_code_block_directive
-# Below is to make pyflakes happy (sigh)
-pygments_code_block_directive
-from youtube import youtube
+from .youtube import youtube
directives.register_directive('youtube', youtube)
+from .slides import slides
+directives.register_directive('slides', slides)
from nikola.plugin_categories import PageCompiler
diff --git a/nikola/plugins/compile_rest/pygments_code_block_directive.py b/nikola/plugins/compile_rest/pygments_code_block_directive.py
index ac91f3c..a83098f 100644
--- a/nikola/plugins/compile_rest/pygments_code_block_directive.py
+++ b/nikola/plugins/compile_rest/pygments_code_block_directive.py
@@ -26,6 +26,7 @@
"""Define and register a code-block directive using pygments"""
+from __future__ import unicode_literals
# Requirements
# ------------
@@ -34,7 +35,10 @@
import codecs
from copy import copy
import os
-import urlparse
+try:
+ from urlparse import urlparse, urlunsplit
+except ImportError:
+ from urllib.parse import urlparse, urlunsplit
from docutils import nodes, core
from docutils.parsers.rst import directives
@@ -90,7 +94,7 @@ class DocutilsInterface(object):
def lex(self):
"""Get lexer for language (use text as fallback)"""
try:
- if self.language and unicode(self.language).lower() != 'none':
+ if self.language and str(self.language).lower() != 'none':
lexer = get_lexer_by_name(self.language.lower(),
**self.custom_args
)
@@ -105,7 +109,7 @@ class DocutilsInterface(object):
"""join subsequent tokens of same token-type
"""
tokens = iter(tokens)
- (lasttype, lastval) = tokens.next()
+ (lasttype, lastval) = next(tokens)
for ttype, value in tokens:
if ttype is lasttype:
lastval += value
@@ -143,7 +147,7 @@ def code_block_directive(name, arguments, options, content, lineno,
content = codecs.open(
options['include'], 'r', encoding).read().rstrip()
except (IOError, UnicodeError): # no file or problem reading it
- content = u''
+ content = ''
line_offset = 0
if content:
# here we define the start-at and end-at options
@@ -163,8 +167,22 @@ def code_block_directive(name, arguments, options, content, lineno,
if after_index < 0:
raise state_machine.reporter.severe(
'Problem with "start-at" option of "%s" '
- 'code-block directive:\nText not found.' %
- options['start-at'])
+ 'code-block directive:\nText not found.'
+ % options['start-at'])
+ # patch mmueller start
+ # Move the after_index to the beginning of the line with the
+ # match.
+ for char in content[after_index:0:-1]:
+ # codecs always opens binary. This works with '\n',
+ # '\r' and '\r\n'. We are going backwards, so
+ # '\n' is found first in '\r\n'.
+ # Going with .splitlines() seems more appropriate
+ # but needs a few more changes.
+ if char == '\n' or char == '\r':
+ break
+ after_index -= 1
+ # patch mmueller end
+
content = content[after_index:]
line_offset = len(content[:after_index].splitlines())
@@ -208,7 +226,7 @@ def code_block_directive(name, arguments, options, content, lineno,
content = content[:before_index]
else:
- content = u'\n'.join(content)
+ content = '\n'.join(content)
if 'tabsize' in options:
tabw = options['tabsize']
@@ -240,7 +258,7 @@ def code_block_directive(name, arguments, options, content, lineno,
else:
# The [:-1] is because pygments adds a trailing \n which looks bad
l = list(DocutilsInterface(content, language, options))
- if l[-1] == ('', u'\n'):
+ if l[-1] == ('', '\n'):
l = l[:-1]
for cls, value in l:
if withln and "\n" in value:
@@ -249,7 +267,7 @@ def code_block_directive(name, arguments, options, content, lineno,
# The first piece, pass as-is
code_block += nodes.Text(values[0], values[0])
# On the second and later pieces, insert \n and linenos
- linenos = range(lineno, lineno + len(values))
+ linenos = list(range(lineno, lineno + len(values)))
for chunk, ln in zip(values, linenos)[1:]:
if ln <= total_lines:
code_block += nodes.inline(fstr % ln, fstr % ln,
@@ -319,7 +337,7 @@ def listings_directive(name, arguments, options, content, lineno,
content_offset, block_text, state, state_machine):
fname = arguments[0]
options['include'] = os.path.join('listings', fname)
- target = urlparse.urlunsplit(("link", 'listing', fname, '', ''))
+ target = urlunsplit(("link", 'listing', fname, '', ''))
generated_nodes = [core.publish_doctree('`%s <%s>`_' % (fname, target))[0]]
generated_nodes += code_block_directive(name, [arguments[1]],
options, content, lineno, content_offset, block_text,
diff --git a/nikola/plugins/compile_rest/slides.py b/nikola/plugins/compile_rest/slides.py
new file mode 100644
index 0000000..942a7d4
--- /dev/null
+++ b/nikola/plugins/compile_rest/slides.py
@@ -0,0 +1,89 @@
+# Copyright (c) 2012 Roberto Alsina y otros.
+
+# Permission is hereby granted, free of charge, to any
+# person obtaining a copy of this software and associated
+# documentation files (the "Software"), to deal in the
+# Software without restriction, including without limitation
+# the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the
+# Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice
+# shall be included in all copies or substantial portions of
+# the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
+# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
+# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
+# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+import json
+
+from docutils import nodes
+from docutils.parsers.rst import Directive, directives
+
+class slides(Directive):
+
+ """ Restructured text extension for inserting slideshows."""
+
+ has_content = True
+ option_spec = {
+ "preload": directives.flag,
+ "preloadImage": directives.uri,
+ "container": directives.unchanged,
+ "generateNextPrev": directives.flag,
+ "next": directives.unchanged,
+ "prev": directives.unchanged,
+ "pagination": directives.flag,
+ "generatePagination": directives.flag,
+ "paginationClass": directives.unchanged,
+ "currentClass": directives.unchanged,
+ "fadeSpeed": directives.positive_int,
+ "fadeEasing": directives.unchanged,
+ "slideSpeed": directives.positive_int,
+ "slideEasing": directives.unchanged,
+ "start": directives.positive_int,
+ "effect": directives.unchanged,
+ "crossfade": directives.flag,
+ "randomize": directives.flag,
+ "play": directives.positive_int,
+ "pause": directives.positive_int,
+ "hoverPause": directives.flag,
+ "autoHeight": directives.flag,
+ "autoHeightSpeed": directives.positive_int,
+ "bigTarget": directives.flag,
+ "animationStart": directives.unchanged,
+ "animationComplete": directives.unchanged,
+ }
+
+ def run(self):
+ if len(self.content) == 0:
+ return
+ for opt in ("preload", "generateNextPrev", "pagination", "generatePagination",
+ "crossfade", "randomize", "hoverPause", "autoHeight", "bigTarget"):
+ if opt in self.options:
+ self.options[opt] = True
+ options = {
+ "autoHeight": True,
+ "bigTarget": True,
+ "paginationClass": "pager",
+ "currentClass": "slide-current"
+ }
+ options.update(self.options)
+ options = json.dumps(options)
+ output = []
+ output.append("""<script> $(function(){ $("#slides").slides(%s); }); </script>""" % options)
+ output.append("""<div id="slides" class="slides"><div class="slides_container">""")
+ for image in self.content:
+ output.append("""<div><img src="%s"></div>""" % image)
+ output.append("""</div></div>""")
+
+ return [nodes.raw('', '\n'.join(output), format='html')]
+
+
+directives.register_directive('slides', slides)
diff --git a/nikola/plugins/compile_rest/youtube.py b/nikola/plugins/compile_rest/youtube.py
index 584160b..0765158 100644
--- a/nikola/plugins/compile_rest/youtube.py
+++ b/nikola/plugins/compile_rest/youtube.py
@@ -1,3 +1,27 @@
+# Copyright (c) 2012 Roberto Alsina y otros.
+
+# Permission is hereby granted, free of charge, to any
+# person obtaining a copy of this software and associated
+# documentation files (the "Software"), to deal in the
+# Software without restriction, including without limitation
+# the rights to use, copy, modify, merge, publish,
+# distribute, sublicense, and/or sell copies of the
+# Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice
+# shall be included in all copies or substantial portions of
+# the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
+# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
+# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
+# PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
+# OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
from docutils import nodes
from docutils.parsers.rst import directives