Skip to content

Commit

Permalink
More refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
flywire committed Jul 12, 2020
1 parent 8ad931c commit 465f811
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 92 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,23 @@ Currently supported options are listed below:
is inserted between the content of `captionPrefix` and the actual figure
number.

* `capcaptionClass` (default: `""`):

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

* `captionNumbering` (default: `False`):

Adds a caption number like "Figure 1:" in front of the caption. It's
wrapped in a `<span />` for easier styling.
Adds a caption number like "Figure 1:" to the caption prefix. It's
wrapped in a `<span />` for easier styling.

* `captionNumberClass` (default: `""`):
* `captionPrefixClass` (default: `""`):

The CSS class to add to the `<span />` element generated for the figure
number.
The CSS class to add to the `<span />` element generated for the caption
prefix.

* `captionClass` (default: `""`):

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

* `contentClass` (default: `""`):

The CSS class to add to the generated content element.

* `stripTitle` (default: `False`):

Expand Down
122 changes: 59 additions & 63 deletions caption/caption.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,90 +11,86 @@

class captionTreeprocessor(Treeprocessor):
def __init__(
self,
md,
stripTitle,
captionClass,
capcaptionClass,
captionNumbering,
captionNumberClass,
captionPrefix):
self,
md,
captionPrefix,
captionNumbering,
captionPrefixClass,
captionClass,
contentClass,
stripTitle):
self.md = md
self.stripTitle = stripTitle
self.captionClass = captionClass
self.capcaptionClass = capcaptionClass
self.captionPrefix = captionPrefix
self.captionNumbering = captionNumbering
self.captionNumber = 0
self.captionNumberClass = captionNumberClass
self.captionPrefix = captionPrefix
self.captionPrefixClass = captionPrefixClass
self.captionClass = captionClass
self.contentClass = contentClass
self.stripTitle = stripTitle

def buildContentElement(self, par):
attrib = par.attrib
par.clear()
par.tag = "figure"
for k, v in attrib.items():
par.set(k, v)
if self.contentClass is not "":
par.set("class", self.contentClass)
par.set("id", "_caption-{}".format(self.captionNumber))
par.text = "\n"
par.tail = "\n"


def buildCaptionElement(self, par, title):
capcaption = ElementTree.SubElement(par, "figcaption")
if self.captionClass is not "":
capcaption.set("class", self.captionClass)
if self.captionNumbering:
captionPrefixSpan = ElementTree.SubElement(capcaption, "span")
captionPrefixSpan.text = "{}&nbsp;{}:".format(self.captionPrefix, self.captionNumber)
captionPrefixSpan.tail = " {}".format(title)
if self.captionPrefixClass is not "":
captionPrefix.set("class", self.captionPrefixClass)
else:
capcaption.text = title
capcaption.tail = "\n"

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

attrib = par.attrib
img = par.find("img")
title = img.get("title")

par.clear()
par.tag = "figure"

# Allow caption before or after

# Object starts here
for k, v in attrib.items():
par.set(k, v)
if self.captionClass is not "":
par.set("class", self.captionClass)
par.text = "\n"

self.buildContentElement(par)
img.tail = "\n"
# Images only
if self.stripTitle:
del img.attrib["title"]
par.append(img)

# Caption starts here
capcaption = ElementTree.SubElement(par, "figcaption")
if self.capcaptionClass is not "":
capcaption.set("class", self.capcaptionClass)

if self.captionNumbering:
captionNumberSpan = ElementTree.SubElement(capcaption, "span")
captionNumberSpan.text = "{}&nbsp;{}:".format(self.captionPrefix, self.captionNumber)
captionNumberSpan.tail = " {}".format(title)
if self.captionNumberClass is not "":
captionNumberSpan.set("class", self.captionNumberClass)
else:
capcaption.text = title

capcaption.tail = "\n"
self.buildCaptionElement(par, img.get("title"))

class captionExtension(Extension):
def __init__(self, **kwargs):
self.config = {
"stripTitle" : [False, "Strip the title from the <img />."],
"captionClass" : ["", "CSS class to add to the <caption /> element."],
"capcaptionClass" : ["", "CSS class to add to the <capcaption /> element."],
"captionNumbering" : [False, "Show the caption number in front of the image caption."],
"captionNumberClass" : ["", "CSS class to add to the caption number <span /> element."],
"captionPrefix" : ["Figure", "The text to show at the front of the caption."],
"captionPrefix" : ["Figure", "The text to show in front of the image caption."],
"captionNumbering" : [False, "Add the caption number to the prefix."],
"captionPrefixClass" : ["", "CSS class to add to the caption prefix <span /> element."],
"captionClass" : ["", "CSS class to add to the caption element."],
"contentClass" : ["", "CSS class to add to the content element."],
"stripTitle" : [False, "Strip the title from the <img />."],
}
super(captionExtension, self).__init__(**kwargs)

def extendMarkdown(self, md):
md.treeprocessors.register(
captionTreeprocessor(
md,
stripTitle=self.getConfig("stripTitle"),
captionClass=self.getConfig("captionClass"),
capcaptionClass=self.getConfig("capcaptionClass"),
captionNumbering=self.getConfig("captionNumbering"),
captionNumberClass=self.getConfig("captionNumberClass"),
captionPrefix=self.getConfig("captionPrefix"),
),
"captiontreeprocessor",
15)
captionTreeprocessor(
md,
captionPrefix=self.getConfig("captionPrefix"),
captionNumbering=self.getConfig("captionNumbering"),
captionPrefixClass=self.getConfig("captionPrefixClass"),
captionClass=self.getConfig("captionClass"),
contentClass=self.getConfig("contentClass"),
stripTitle=self.getConfig("stripTitle"),
),
"captiontreeprocessor",
8)

def makeExtension(**kwargs):
return captionExtension(**kwargs)
60 changes: 41 additions & 19 deletions test/test_caption.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_simple_image(self):
inString = """\
![alt text](/path/to/image.png "Title")"""
expectedString = """\
<figure>
<figure id="_figure-1">
<img alt="alt text" src="/path/to/image.png" title="Title" />
<figcaption>Title</figcaption>
</figure>"""
Expand All @@ -56,7 +56,7 @@ def test_multiline_alt(self):
![This is a rather long alt text that spans multiple lines. This may be
necessary to describe a picture for the blind.](/path/to/image.png "Title")"""
expectedString = """\
<figure>
<figure id="_figure-1">
<img alt="This is a rather long alt text that spans multiple lines. This may be
necessary to describe a picture for the blind." src="/path/to/image.png" title="Title" />
<figcaption>Title</figcaption>
Expand All @@ -70,7 +70,7 @@ def test_multiline_title(self):
the readers a good figcaption. It may contain a description of the image as well
as sources.")"""
expectedString = """\
<figure>
<figure id="_figure-1">
<img alt="alt text" src="/path/to/image.png" title="This is a very long title. It is used to give
the readers a good figcaption. It may contain a description of the image as well
as sources." />
Expand All @@ -83,7 +83,7 @@ def test_strip_title(self):
inString = """\
![alt text](/path/to/image.png "Title")"""
expectedString = """\
<figure>
<figure id="_figure-1">
<img alt="alt text" src="/path/to/image.png" />
<figcaption>Title</figcaption>
</figure>"""
Expand All @@ -94,22 +94,22 @@ def test_figure_class(self):
inString = """\
![alt text](/path/to/image.png "Title")"""
expectedString = """\
<figure class="testclass">
<figure class="testclass" id="_figure-1">
<img alt="alt text" src="/path/to/image.png" title="Title" />
<figcaption>Title</figcaption>
</figure>"""
outString = markdown.markdown(inString, extensions = [caption.captionExtension(figureClass="testclass")])
outString = markdown.markdown(inString, extensions = [caption.captionExtension(contentClass="testclass")])
self.assertEqual(expectedString, outString)

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

def test_figure_numbering(self):
Expand All @@ -118,46 +118,68 @@ def test_figure_numbering(self):
![alt text 2](/path/to/image2.png "Title 2")"""
expectedString = """\
<figure>
<figure id="_figure-1">
<img alt="alt text" src="/path/to/image.png" title="Title" />
<figcaption><span>Figure&nbsp;1:</span> Title</figcaption>
</figure>
<figure>
<figure id="_figure-1">
<img alt="alt text 2" src="/path/to/image2.png" title="Title 2" />
<figcaption><span>Figure&nbsp;2:</span> Title 2</figcaption>
</figure>"""
outString = markdown.markdown(inString, extensions = [caption.captionExtension(figureNumbering=True)])
outString = markdown.markdown(inString, extensions = [caption.captionExtension(captionNumbering=True)])
self.assertEqual(expectedString, outString)

def test_figure_number_class(self):
inString = """\
![alt text](/path/to/image.png "Title")"""
expectedString = """\
<figure>
<figure id="_figure-1">
<img alt="alt text" src="/path/to/image.png" title="Title" />
<figcaption><span class="testclass">Figure&nbsp;1:</span> Title</figcaption>
</figure>"""
outString = markdown.markdown(inString, extensions = [caption.captionExtension(figureNumbering=True, figureNumberClass="testclass")])
outString = markdown.markdown(inString, extensions = [caption.captionExtension(captionNumbering=True, captionPrefixClass="testclass")])
self.assertEqual(expectedString, outString)

def test_figure_number_text(self):
inString = """\
![alt text](/path/to/image.png "Title")"""
expectedString = """\
<figure>
<figure id="_figure-1">
<img alt="alt text" src="/path/to/image.png" title="Title" />
<figcaption><span>Abbildung&nbsp;1:</span> Title</figcaption>
</figure>"""
outString = markdown.markdown(inString, extensions = [caption.captionExtension(figureNumbering=True, figureNumberText="Abbildung")])
outString = markdown.markdown(inString, extensions = [caption.captionExtension(captionNumbering=True, captionPrefix="Abbildung")])
self.assertEqual(expectedString, outString)

def test_attribute_preservation(self):
inString = """\
![alt text](/path/to/image.png "Title"){: #someid .someclass somekey='some value' }"""
expectedString = """\
<figure id="_figure-1">
<img alt="alt text" class="someclass" id="someid" somekey="some value" src="/path/to/image.png" title="Title" />
<figcaption>Title</figcaption>
</figure>"""
outString = markdown.markdown(inString, extensions = ["attr_list", caption.captionExtension()])
self.assertEqual(expectedString, outString)

def test_image_in_link(self):
inString = """\
[![alt text](/path/to/image.png "Title")](/path/to/link.html)"""
expectedString = """\
<figure id="_figure-1">
<a href="/path/to/link.html"><img alt="alt text" src="/path/to/image.png" title="Title" /></a>
<figcaption>Title</figcaption>
</figure>"""
outString = markdown.markdown(inString, extensions = [caption.captionExtension()])
self.assertEqual(expectedString, outString)

def test_combined_options(self):
inString = """\
![alt text](/path/to/image.png "Title")"""
[![alt text](/path/to/image.png "Title"){: #someid .someclass somekey='some value' }](/path/to/link.html)"""
expectedString = """\
<figure class="testclass1">
<img alt="alt text" src="/path/to/image.png" />
<figure class="testclass1" id="_figure-1">
<a href="/path/to/link.html"><img alt="alt text" class="someclass" id="someid" somekey="some value" src="/path/to/image.png" /></a>
<figcaption class="testclass2"><span class="testclass3">Abbildung&nbsp;1:</span> Title</figcaption>
</figure>"""
outString = markdown.markdown(inString, extensions = [caption.captionExtension(stripTitle=True, figureClass="testclass1", figcaptionClass="testclass2", figureNumbering=True, figureNumberClass="testclass3", figureNumberText="Abbildung")])
outString = markdown.markdown(inString, extensions = ["attr_list", caption.captionExtension(stripTitle=True, contentClass="testclass1", captionClass="testclass2", captionNumbering=True, captionPrefixClass="testclass3", captionPrefix="Abbildung")])
self.assertEqual(expectedString, outString)

0 comments on commit 465f811

Please sign in to comment.