~ferruck/yafg

54e6ba18105effd3f9f131b3f56a5c5b4dfb16f0 — Philipp Trommler 28 days ago 43a2186
Refactor

Move the building of the <figure /> and <figcaption /> elements into
subfunctions to improve readability.
1 files changed, 23 insertions(+), 22 deletions(-)

M yafg/yafg.py
M yafg/yafg.py => yafg/yafg.py +23 -22
@@ 38,17 38,8 @@ class YafgTreeprocessor(Treeprocessor):
                    a = None
        return (img, a)

    def run(self, root):
        for par in root.findall("./p"):
            img, a = YafgTreeprocessor.matchChildren(par)
            if img is None:
                continue

            self.figureNumber += 1

    def buildFigureElement(self, par):
            attrib = par.attrib
            title = img.get("title")

            par.clear()
            par.tag = "figure"
            for k, v in attrib.items():


@@ 59,20 50,10 @@ class YafgTreeprocessor(Treeprocessor):
            par.text = "\n"
            par.tail = "\n"

            if self.stripTitle:
                del img.attrib["title"]

            if a is not None:
                a.tail = "\n"
                par.append(a)
            else:
                img.tail = "\n"
                par.append(img)

    def buildFigcaptionElement(self, par, title):
            figcaption = ElementTree.SubElement(par, "figcaption")
            if self.figcaptionClass is not "":
                figcaption.set("class", self.figcaptionClass)

            if self.figureNumbering:
                figureNumberSpan = ElementTree.SubElement(figcaption, "span")
                figureNumberSpan.text = "{}&nbsp;{}:".format(self.figureNumberText, self.figureNumber)


@@ 81,9 62,29 @@ class YafgTreeprocessor(Treeprocessor):
                    figureNumberSpan.set("class", self.figureNumberClass)
            else:
                figcaption.text = title

            figcaption.tail = "\n"

    def run(self, root):
        for par in root.findall("./p"):
            img, a = YafgTreeprocessor.matchChildren(par)
            if img is None:
                continue

            self.figureNumber += 1

            self.buildFigureElement(par)
            if a is not None:
                a.tail = "\n"
                par.append(a)
            else:
                img.tail = "\n"
                par.append(img)
            self.buildFigcaptionElement(par, img.get("title"))

            if self.stripTitle:
                del img.attrib["title"]


class YafgExtension(Extension):
    def __init__(self, **kwargs):
        self.config = {