From 346d026e55ff91e7dbd9d1aeab1ed9dfc0cfe93e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sun, 26 Aug 2018 18:03:15 +0200 Subject: [PATCH] plugins: new m.link plugin. --- doc/plugins.rst | 5 +-- doc/plugins/links.rst | 29 ++++++++++++++++ pelican-plugins/m/abbr.py | 2 +- pelican-plugins/m/gh.py | 2 +- pelican-plugins/m/gl.py | 2 +- pelican-plugins/m/link.py | 50 +++++++++++++++++++++++++++ pelican-plugins/m/test/link/page.html | 38 ++++++++++++++++++++ pelican-plugins/m/test/link/page.rst | 9 +++++ pelican-plugins/m/test/test_link.py | 36 +++++++++++++++++++ pelican-plugins/m/vk.py | 2 +- site/pelicanconf.py | 1 + 11 files changed, 170 insertions(+), 6 deletions(-) create mode 100644 pelican-plugins/m/link.py create mode 100644 pelican-plugins/m/test/link/page.html create mode 100644 pelican-plugins/m/test/link/page.rst create mode 100644 pelican-plugins/m/test/test_link.py diff --git a/doc/plugins.rst b/doc/plugins.rst index 1ab7289f..8404bd5f 100644 --- a/doc/plugins.rst +++ b/doc/plugins.rst @@ -47,7 +47,8 @@ and restart Pelican. Download the plugins below or :gh:`m.code ` (needs also :gh:`ansilexer `) - :gh:`m.plots `, :gh:`m.dot ` -- :gh:`m.gh `, +- :gh:`m.link `, + :gh:`m.gh `, :gh:`m.dox `, :gh:`m.gl `, :gh:`m.vk `, @@ -100,7 +101,7 @@ else. `Links and other » <{filename}/plugins/links.rst>`_ =================================================== -The :py:`m.gh`, :py:`m.dox`, :py:`m.gl`, :py:`m.vk`, :py:`m.abbr`, +The :py:`m.link`, :py:`m.gh`, :py:`m.dox`, :py:`m.gl`, :py:`m.vk`, :py:`m.abbr`, :py:`m.fiilesize` and :py:`m.alias` plugins make it easy for you to link to GitHub projects, issues or PRs, to Doxygen documentation, query file sizes and provide URL aliases to preserve link compatibility. diff --git a/doc/plugins/links.rst b/doc/plugins/links.rst index d9f3ed5b..3669aed4 100644 --- a/doc/plugins/links.rst +++ b/doc/plugins/links.rst @@ -50,6 +50,35 @@ own requirements. .. contents:: :class: m-block m-default +`Stylable links`_ +================= + +Download the `m/link.py <{filename}/plugins.rst>`_ file, put it including the +``m/`` directory into one of your :py:`PLUGIN_PATHS` and add :py:`m.link` +package to your :py:`PLUGINS` in ``pelicanconf.py``: + +.. code:: python + + PLUGINS += ['m.link'] + +The :rst:`:link:` interpreted text role is an alternative to the builtin +:abbr:`reST ` link syntax, but with an ability to specify +additional CSS classes. At the moment the plugin knows only external URLs. + +.. code-figure:: + + .. code:: rst + + .. role:: link-flat-big(link) + :class: m-flat m-text m-big + + :link-flat-big:`Look here, this is great! ` + + .. role:: link-flat-big(link) + :class: m-flat m-text m-big + + :link-flat-big:`Look here, this is great! ` + `GitHub`_ ========= diff --git a/pelican-plugins/m/abbr.py b/pelican-plugins/m/abbr.py index 812e12a8..feaab9ec 100644 --- a/pelican-plugins/m/abbr.py +++ b/pelican-plugins/m/abbr.py @@ -28,7 +28,7 @@ from docutils.parsers import rst from docutils.parsers.rst.roles import set_classes # to avoid dependencies, link_regexp and parse_link() is common for m.abbr, -# m.gh, m.gl and m.vk +# m.gh, m.gl, m.link and m.vk link_regexp = re.compile(r'(?P.*) <(?P<link>.+)>') diff --git a/pelican-plugins/m/gh.py b/pelican-plugins/m/gh.py index a74ace4f..73502697 100644 --- a/pelican-plugins/m/gh.py +++ b/pelican-plugins/m/gh.py @@ -28,7 +28,7 @@ from docutils.parsers import rst from docutils.parsers.rst.roles import set_classes # to avoid dependencies, link_regexp and parse_link() is common for m.abbr, -# m.gh, m.gl and m.vk +# m.gh, m.gl, m.link and m.vk link_regexp = re.compile(r'(?P<title>.*) <(?P<link>.+)>') diff --git a/pelican-plugins/m/gl.py b/pelican-plugins/m/gl.py index 0e89331f..7297d845 100644 --- a/pelican-plugins/m/gl.py +++ b/pelican-plugins/m/gl.py @@ -28,7 +28,7 @@ from docutils.parsers import rst from docutils.parsers.rst.roles import set_classes # to avoid dependencies, link_regexp and parse_link() is common for m.abbr, -# m.gh, m.gl and m.vk +# m.gh, m.gl, m.link and m.vk link_regexp = re.compile(r'(?P<title>.*) <(?P<link>.+)>') diff --git a/pelican-plugins/m/link.py b/pelican-plugins/m/link.py new file mode 100644 index 00000000..1de1afb6 --- /dev/null +++ b/pelican-plugins/m/link.py @@ -0,0 +1,50 @@ +# +# This file is part of m.css. +# +# Copyright © 2017, 2018 Vladimír Vondruš <mosra@centrum.cz> +# +# 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 re +from docutils import nodes, utils +from docutils.parsers import rst +from docutils.parsers.rst.roles import set_classes + +# to avoid dependencies, link_regexp and parse_link() is common for m.abbr, +# m.gh, m.gl, m.link and m.vk + +link_regexp = re.compile(r'(?P<title>.*) <(?P<link>.+)>') + +def parse_link(text): + link = utils.unescape(text) + m = link_regexp.match(link) + if m: return m.group('title', 'link') + return None, link + +def link(name, rawtext, text, lineno, inliner, options={}, content=[]): + title, url = parse_link(text) + if not title: title = url + # TODO: mailto URLs, internal links (need to gut out docutils for that) + set_classes(options) + node = nodes.reference(rawtext, title, refuri=url, **options) + return [node], [] + +def register(): + rst.roles.register_local_role('link', link) diff --git a/pelican-plugins/m/test/link/page.html b/pelican-plugins/m/test/link/page.html new file mode 100644 index 00000000..4960f004 --- /dev/null +++ b/pelican-plugins/m/test/link/page.html @@ -0,0 +1,38 @@ +<!DOCTYPE html> +<html lang="en"> +<head> + <meta charset="UTF-8" /> + <title>m.link | A Pelican Blog + + + + + + +
+
+ +
+ + diff --git a/pelican-plugins/m/test/link/page.rst b/pelican-plugins/m/test/link/page.rst new file mode 100644 index 00000000..5ca6a125 --- /dev/null +++ b/pelican-plugins/m/test/link/page.rst @@ -0,0 +1,9 @@ +m.link +###### + +.. role:: link-flat(link) + :class: m-flat m-text m-strong + +- URL with no title: :link:`http://mcss.mosra.cz` +- URL with a title: :link:`m.css ` +- Link with a class: :link-flat:`https://twitter.com/czmosra` diff --git a/pelican-plugins/m/test/test_link.py b/pelican-plugins/m/test/test_link.py new file mode 100644 index 00000000..e893ac8f --- /dev/null +++ b/pelican-plugins/m/test/test_link.py @@ -0,0 +1,36 @@ +# +# This file is part of m.css. +# +# Copyright © 2017, 2018 Vladimír Vondruš +# +# 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 . import PluginTestCase + +class Link(PluginTestCase): + def __init__(self, *args, **kwargs): + super().__init__(__file__, '', *args, **kwargs) + + def test(self): + self.run_pelican({ + 'PLUGINS': ['m.htmlsanity', 'm.link'] + }) + + self.assertEqual(*self.actual_expected_contents('page.html')) diff --git a/pelican-plugins/m/vk.py b/pelican-plugins/m/vk.py index 8fad35f2..1cbf5457 100644 --- a/pelican-plugins/m/vk.py +++ b/pelican-plugins/m/vk.py @@ -28,7 +28,7 @@ from docutils.parsers import rst from docutils.parsers.rst.roles import set_classes # to avoid dependencies, link_regexp and parse_link() is common for m.abbr, -# m.gh, m.gl and m.vk +# m.gh, m.gl, m.link and m.vk link_regexp = re.compile(r'(?P.*) <(?P<link>.+)>') diff --git a/site/pelicanconf.py b/site/pelicanconf.py index de0ebdbc..cfcb7335 100644 --- a/site/pelicanconf.py +++ b/site/pelicanconf.py @@ -147,6 +147,7 @@ PLUGINS = ['m.abbr', 'm.gh', 'm.htmlsanity', 'm.images', + 'm.link', 'm.math', 'm.metadata', 'm.plots', -- 2.30.2