From b86e17607c13e5ae1a607f589fcf7ae1d4000709 Mon Sep 17 00:00:00 2001 From: Samy Arous Date: Sun, 16 Feb 2014 19:23:48 +0100 Subject: [PATCH] Initial release of the twitter bootstrap rst directives plugin --- twitter_bootstrap_rst_directives/Demo.rst | 118 ++++ twitter_bootstrap_rst_directives/Readme.rst | 46 ++ twitter_bootstrap_rst_directives/__init__.py | 4 + .../bootstrap_rst_directives.py | 543 ++++++++++++++++++ 4 files changed, 711 insertions(+) create mode 100644 twitter_bootstrap_rst_directives/Demo.rst create mode 100644 twitter_bootstrap_rst_directives/Readme.rst create mode 100644 twitter_bootstrap_rst_directives/__init__.py create mode 100644 twitter_bootstrap_rst_directives/bootstrap_rst_directives.py diff --git a/twitter_bootstrap_rst_directives/Demo.rst b/twitter_bootstrap_rst_directives/Demo.rst new file mode 100644 index 0000000..c38ae10 --- /dev/null +++ b/twitter_bootstrap_rst_directives/Demo.rst @@ -0,0 +1,118 @@ +This will be turned into :abbr:`HTML (HyperText Markup Language)`. + +Love this music :glyph:`music` + +.. role:: story_time_glyph(glyph) + :target: http://www.youtube.com/watch?v=5g8ykQLYnX0 + :class: small text-info + +Love this music :story_time_glyph:`music` + +This is an example of code: :code:``. + +This is an example of kbd: :kbd:``. + + +.. label-default:: + + This is a default label content + +.. label-primary:: + + This is a primary label content + +.. label-success:: + + This is a success label content + +.. label-info:: + + This is a info label content + +.. label-warning:: + + This is a warning label content + +.. label-danger:: + + This is a danger label content + + +.. panel-default:: + :title: panel default title + + This is a default panel content + +.. panel-primary:: + :title: panel primary title + + This is a primary panel content + +.. panel-success:: + :title: panel success title + + This is a success panel content + +.. panel-info:: + :title: panel info title + + This is a info panel content + +.. panel-warning:: + :title: panel warning title + + This is a warning panel content + +.. panel-danger:: + :title: panel danger title + + This is a danger panel content + + +.. alert-success:: + + This is a success alert content + +.. alert-info:: + + This is a info alert content + +.. alert-warning:: + + This is a warning alert content + +.. alert-danger:: + + This is a danger alert content + + +.. media:: http://stuffkit.com/wp-content/uploads/2012/11/Worlds-Most-Beautiful-Lady-Camilla-Belle-HD-Photos-4.jpg + :height: 750 + :width: 1000 + :scale: 20 + :target: http://www.google.com + :alt: Camilla Belle + :position: left + + .. class:: h3 + + left position + + This image is not mine. Credit goes to http://stuffkit.com + + + +.. media:: http://stuffkit.com/wp-content/uploads/2012/11/Worlds-Most-Beautiful-Lady-Camilla-Belle-HD-Photos-4.jpg + :height: 750 + :width: 1000 + :scale: 20 + :target: http://www.google.com + :alt: Camilla Belle + :position: right + + .. class:: h3 + + right position + + + This image is not mine. Credit goes to http://stuffkit.com \ No newline at end of file diff --git a/twitter_bootstrap_rst_directives/Readme.rst b/twitter_bootstrap_rst_directives/Readme.rst new file mode 100644 index 0000000..f84417c --- /dev/null +++ b/twitter_bootstrap_rst_directives/Readme.rst @@ -0,0 +1,46 @@ +Twitter Bootstrap Directive for restructured text +------------------------------------------------- + +This plugin defines some rst directive that enable a clean usage of the twitter bootstrap CSS and Javascript components. + +Directives +---------- + +Implemented directives: + + label, + alert, + panel, + media + +Implemented roles: + + glyph, + code, + kbd + +Usage +----- + +For more informations about the usage of each directive, read the corresponding class description. +Or checkout this demo page. + +Dependencies +------------ + +In order to use this plugin, you need to use a template that supports bootstrap 3.1.1 with the glyph font setup +correctly. Usually you should have this structure:: + + static + ├── css + | └── bootstrap.min.css + ├── font + | └── glyphicons-halflings-regular.ttf + └── js + └── + +Warning +------- + +In order to support some unique features and avoid conflicts with bootstrap, this plugin will use a custom html writer which +is modifying the traditional docutils output. \ No newline at end of file diff --git a/twitter_bootstrap_rst_directives/__init__.py b/twitter_bootstrap_rst_directives/__init__.py new file mode 100644 index 0000000..7dfdc27 --- /dev/null +++ b/twitter_bootstrap_rst_directives/__init__.py @@ -0,0 +1,4 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from .bootstrap_rst_directives import * diff --git a/twitter_bootstrap_rst_directives/bootstrap_rst_directives.py b/twitter_bootstrap_rst_directives/bootstrap_rst_directives.py new file mode 100644 index 0000000..8385134 --- /dev/null +++ b/twitter_bootstrap_rst_directives/bootstrap_rst_directives.py @@ -0,0 +1,543 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + + +""" +Twitter Bootstrap RST directives Plugin For Pelican +=================================================== + +This plugin defines rst directives for different CSS and Javascript components from +the twitter bootstrap framework. + +""" + +from uuid import uuid1 + +from cgi import escape +from docutils import nodes, utils +import docutils +from docutils.parsers import rst +from docutils.parsers.rst import directives, roles, Directive +from pelican import signals +from pelican.readers import RstReader, PelicanHTMLTranslator + + + +class CleanHTMLTranslator(PelicanHTMLTranslator): + + """ + A custom HTML translator based on the Pelican HTML translator. + Used to clean up some components html classes that could conflict + with the bootstrap CSS classes. + Also defines new tags that are not handleed by the current implementation of + docutils. + + The most obvious example is the Container component + """ + + def visit_literal(self, node): + classes = node.get('classes', node.get('class', [])) + if 'code' in classes: + self.body.append(self.starttag(node, 'code')) + elif 'kbd' in classes: + self.body.append(self.starttag(node, 'kbd')) + else: + self.body.append(self.starttag(node, 'pre')) + + def depart_literal(self, node): + classes = node.get('classes', node.get('class', [])) + if 'code' in classes: + self.body.append('\n') + elif 'kbd' in classes: + self.body.append('\n') + else: + self.body.append('\n') + + def visit_container(self, node): + self.body.append(self.starttag(node, 'div')) + + +class CleanRSTReader(RstReader): + + """ + A custom RST reader that behaves exactly like its parent class RstReader with + the difference that it uses the CleanHTMLTranslator + """ + + def _get_publisher(self, source_path): + extra_params = {'initial_header_level': '2', + 'syntax_highlight': 'short', + 'input_encoding': 'utf-8'} + user_params = self.settings.get('DOCUTILS_SETTINGS') + if user_params: + extra_params.update(user_params) + + pub = docutils.core.Publisher( + destination_class=docutils.io.StringOutput) + pub.set_components('standalone', 'restructuredtext', 'html') + pub.writer.translator_class = CleanHTMLTranslator + pub.process_programmatic_settings(None, extra_params, None) + pub.set_source(source_path=source_path) + pub.publish() + return pub + + +def keyboard_role(name, rawtext, text, lineno, inliner, + options={}, content=[]): + """ + This function creates an inline console input block as defined in the twitter bootstrap documentation + overrides the default behaviour of the kbd role + + *usage:* + :kbd:`` + + *Example:* + + :kbd:`
` + + This code is not highlighted + """ + new_element = nodes.literal(rawtext, text) + new_element.set_class('kbd') + + return [new_element], [] + + +def code_role(name, rawtext, text, lineno, inliner, + options={}, content=[]): + """ + This function creates an inline code block as defined in the twitter bootstrap documentation + overrides the default behaviour of the code role + + *usage:* + :code:`` + + *Example:* + + :code:`
` + + This code is not highlighted + """ + new_element = nodes.literal(rawtext, text) + new_element.set_class('code') + + return [new_element], [] + + +def glyph_role(name, rawtext, text, lineno, inliner, + options={}, content=[]): + """ + This function defines a glyph inline role that show a glyph icon from the + twitter bootstrap framework + + *Usage:* + + :glyph:`` + + *Example:* + + Love this music :glyph:`music` :) + + Can be subclassed to include a target + + *Example:* + + .. role:: story_time_glyph(glyph) + :target: http://www.youtube.com/watch?v=5g8ykQLYnX0 + :class: small text-info + + Love this music :story_time_glyph:`music` :) + + """ + + target = options.get('target', None) + glyph_name = 'glyphicon-{}'.format(text) + + if target: + target = utils.unescape(target) + new_element = nodes.reference(rawtext, ' ', refuri=target) + else: + new_element = nodes.container() + classes = options.setdefault('class', []) + classes += ['glyphicon', glyph_name] + for custom_class in classes: + new_element.set_class(custom_class) + return [new_element], [] + +glyph_role.options = { + 'target': rst.directives.unchanged, +} +glyph_role.content = False + + +class Label(rst.Directive): + + ''' + generic Label directive class definition. + This class define a directive that shows + bootstrap Labels around its content + + *usage:* + + .. label-:: + +