[Sync] Kazimir: img -> figure with caption
This commit is contained in:
1 parent
5f732d8468
commit
d6a3cb99eb
2 files changed
+46
No files matched your search
@@ -3,11 +3,13 @@ import requests
|
|||||||
from markdown import markdown
|
from markdown import markdown
|
||||||
|
|
||||||
from kazimir.video import VideoExtension
|
from kazimir.video import VideoExtension
|
||||||
|
from kazimir.figure import FigureExtension
|
||||||
from kazimir.instagram import InstagramExtension
|
from kazimir.instagram import InstagramExtension
|
||||||
|
|
||||||
|
|
||||||
def markdown_to_html(text):
|
def markdown_to_html(text):
|
||||||
return markdown(text, extensions=[
|
return markdown(text, extensions=[
|
||||||
|
FigureExtension(),
|
||||||
VideoExtension(),
|
VideoExtension(),
|
||||||
InstagramExtension(),
|
InstagramExtension(),
|
||||||
])
|
])
|
||||||
|
|||||||
@@ -0,0 +1,44 @@
|
|||||||
|
from markdown import Extension
|
||||||
|
from markdown.inlinepatterns import IMAGE_LINK_RE, IMAGE_REFERENCE_RE
|
||||||
|
from markdown.blockprocessors import BlockProcessor
|
||||||
|
from markdown.util import etree
|
||||||
|
import re
|
||||||
|
|
||||||
|
FIGURES = [
|
||||||
|
u'^\s*' + IMAGE_LINK_RE + u'\s*$',
|
||||||
|
u'^\s*' + IMAGE_REFERENCE_RE + u'\s*$',
|
||||||
|
]
|
||||||
|
|
||||||
|
TAG_FIGURE = 'figure'
|
||||||
|
TAG_FIGCAPTION = 'figcaption'
|
||||||
|
NAME = 'FigureAltCaption'
|
||||||
|
|
||||||
|
|
||||||
|
class FigureProcessor(BlockProcessor):
|
||||||
|
FIGURES_RE = re.compile('|'.join(f for f in FIGURES))
|
||||||
|
|
||||||
|
def test(self, parent, block):
|
||||||
|
is_image = bool(self.FIGURES_RE.search(block))
|
||||||
|
is_single_list = (len(block.splitlines()) == 1)
|
||||||
|
is_in_figure = parent.tag == TAG_FIGURE
|
||||||
|
|
||||||
|
return is_image and is_single_list and not is_in_figure
|
||||||
|
|
||||||
|
def run(self, parent, blocks):
|
||||||
|
raw_block = blocks.pop(0)
|
||||||
|
caption_text = self.FIGURES_RE.search(raw_block).group(1)
|
||||||
|
|
||||||
|
figure = etree.SubElement(parent, TAG_FIGURE)
|
||||||
|
figure.text = raw_block
|
||||||
|
|
||||||
|
figcaption_elem = etree.SubElement(figure, TAG_FIGCAPTION)
|
||||||
|
figcaption_elem.text = caption_text
|
||||||
|
|
||||||
|
|
||||||
|
class FigureExtension(Extension):
|
||||||
|
def extendMarkdown(self, md, md_globals):
|
||||||
|
md.parser.blockprocessors.add(
|
||||||
|
NAME,
|
||||||
|
FigureProcessor(md.parser),
|
||||||
|
'<ulist'
|
||||||
|
)
|
||||||
Reference in new issue
Block a user