~ferruck/yafg

5c6ae54b3438680b61818ee6b3a88e9c8d0085fb — Philipp Trommler 8 months ago b793599 master v0.2
Add support for CSS classes
4 files changed, 60 insertions(+), 4 deletions(-)

M README.md
M setup.py
M test/test_yafg.py
M yafg/yafg.py
M README.md => README.md +8 -0
@@ 70,6 70,14 @@ Currently supported options are listed below:
    usage is discouraged anyways (see below), this may be an option worth
    considering.

* `figureClass` (default: `""`):

    The CSS class to add to the generated `<figure />` element.

* `figcaptionClass` (default: `""`):

    The CSS class to add to the generated `<figcaption />` element.

## Why?

yafg arose from the dissatisfaction with the existing solutions, namely:

M setup.py => setup.py +1 -1
@@ 44,5 44,5 @@ setuptools.setup(
        packages = setuptools.find_packages(),
        python_requires = ">=2.7.15, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*, !=3.5.*, <4",
        url = "https://git.sr.ht/~ferruck/yafg",
        version = "0.1",
        version = "0.2",
)

M test/test_yafg.py => test/test_yafg.py +33 -0
@@ 87,3 87,36 @@ as sources.</figcaption>
</figure>"""
        outString = markdown.markdown(inString, extensions = [yafg.YafgExtension(stripTitle=True)])
        self.assertEqual(expectedString, outString)

    def test_figure_class(self):
        inString = """\
![alt text](/path/to/image.png "Title")"""
        expectedString = """\
<figure class="testclass">
<img alt="alt text" src="/path/to/image.png" title="Title" />
<figcaption>Title</figcaption>
</figure>"""
        outString = markdown.markdown(inString, extensions = [yafg.YafgExtension(figureClass="testclass")])
        self.assertEqual(expectedString, outString)

    def test_figcaption_class(self):
        inString = """\
![alt text](/path/to/image.png "Title")"""
        expectedString = """\
<figure>
<img alt="alt text" src="/path/to/image.png" title="Title" />
<figcaption class="testclass">Title</figcaption>
</figure>"""
        outString = markdown.markdown(inString, extensions = [yafg.YafgExtension(figcaptionClass="testclass")])
        self.assertEqual(expectedString, outString)

    def test_combined_options(self):
        inString = """\
![alt text](/path/to/image.png "Title")"""
        expectedString = """\
<figure class="testclass1">
<img alt="alt text" src="/path/to/image.png" />
<figcaption class="testclass2">Title</figcaption>
</figure>"""
        outString = markdown.markdown(inString, extensions = [yafg.YafgExtension(stripTitle=True, figureClass="testclass1", figcaptionClass="testclass2")])
        self.assertEqual(expectedString, outString)

M yafg/yafg.py => yafg/yafg.py +18 -3
@@ 8,9 8,11 @@ from markdown.treeprocessors import Treeprocessor
from xml.etree import ElementTree

class YafgTreeprocessor(Treeprocessor):
    def __init__(self, md, stripTitle):
    def __init__(self, md, stripTitle, figureClass, figcaptionClass):
        self.md = md
        self.stripTitle = stripTitle
        self.figureClass = figureClass
        self.figcaptionClass = figcaptionClass

    def run(self, root):
        for par in root.findall("./p[img]"):


@@ 22,6 24,8 @@ class YafgTreeprocessor(Treeprocessor):
            par.tag = "figure"
            for k, v in attrib.items():
                par.set(k, v)
            if self.figureClass is not "":
                par.set("class", self.figureClass)
            par.text = "\n"

            img.tail = "\n"


@@ 30,17 34,28 @@ class YafgTreeprocessor(Treeprocessor):
            par.append(img)

            figcaption = ElementTree.SubElement(par, "figcaption")
            if self.figcaptionClass is not "":
                figcaption.set("class", self.figcaptionClass)
            figcaption.text = title
            figcaption.tail = "\n"

class YafgExtension(Extension):
    def __init__(self, **kwargs):
        self.config = {"stripTitle" : [False, "Strip the title from the <img />."]}
        self.config = {
                "stripTitle" : [False, "Strip the title from the <img />."],
                "figureClass" : ["", "CSS class to add to the <figure /> element."],
                "figcaptionClass" : ["", "CSS class to add to the <figcaption /> element."],
        }
        super(YafgExtension, self).__init__(**kwargs)

    def extendMarkdown(self, md):
        md.treeprocessors.register(
                YafgTreeprocessor(md, stripTitle = self.getConfig("stripTitle")),
                YafgTreeprocessor(
                    md,
                    stripTitle=self.getConfig("stripTitle"),
                    figureClass=self.getConfig("figureClass"),
                    figcaptionClass=self.getConfig("figcaptionClass"),
                ),
                "yafgtreeprocessor",
                15)