本文整理汇总了Python中markdown.util.etree.SubElement方法的典型用法代码示例。如果您正苦于以下问题:Python etree.SubElement方法的具体用法?Python etree.SubElement怎么用?Python etree.SubElement使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类markdown.util.etree
的用法示例。
在下文中一共展示了etree.SubElement方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _build_row
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import SubElement [as 别名]
def _build_row(self, row, parent, align, border):
""" Given a row of text, build table cells. """
tr = etree.SubElement(parent, 'tr')
tag = 'td'
if parent.tag == 'thead':
tag = 'th'
cells = self._split_row(row, border)
# We use align here rather than cells to ensure every row
# contains the same number of columns.
for i, a in enumerate(align):
c = etree.SubElement(tr, tag)
try:
c.text = cells[i].strip()
except IndexError:
c.text = ""
if a:
c.set('align', a)
示例2: run
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import SubElement [as 别名]
def run(self, parent, blocks): # how to process the block?
raw_block = blocks.pop(0)
captionText = self.CAPTION_RE.search(raw_block).group('caption')
try:
attrText = self.ATTR_RE.search(raw_block).group('attributes') # Get the caption text
except:
attrText = None
# create figure
figure = etree.SubElement(parent, 'figure')
if attrText:
figure.set('id',attrText)
# render image in figure
figure.text = raw_block
# create caption
figcaptionElem = etree.SubElement(figure,'figcaption')
figcaptionElem.text = captionText #no clue why the text itself turns out as html again and not raw. Anyhow, it suits me, the blockparsers annoyingly wrapped everything into <p>.
示例3: _build_row
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import SubElement [as 别名]
def _build_row(self, row, parent, align, border):
""" Given a row of text, build table cells. """
tr = etree.SubElement(parent, 'tr')
tag = 'td'
if parent.tag == 'thead':
tag = 'th'
cells = self._split_row(row, border)
# We use align here rather than cells to ensure every row
# contains the same number of columns.
for i, a in enumerate(align):
c = etree.SubElement(tr, tag)
try:
c.text = cells[i].strip()
except IndexError:
c.text = ""
if a:
c.set('align', a)
示例4: makeFootnotesDiv
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import SubElement [as 别名]
def makeFootnotesDiv(self, root):
""" Return div of footnotes as et Element. """
if not list(self.footnotes.keys()):
return None
div = etree.Element("div")
div.set('class', 'footnote')
hr = etree.SubElement(div, "hr")
ol = etree.SubElement(div, "ol")
for id in list(self.footnotes.keys()):
li = etree.SubElement(ol, "li")
li.set("id", self.makeFootnoteId(id))
self.parser.parseChunk(li, self.footnotes[id])
backlink = etree.Element("a")
backlink.set("href", "#" + self.makeFootnoteRefId(id))
backlink.set("rev", "footnote")
backlink.set("title", "Jump back to footnote %d in the text" % \
(self.footnotes.index(id)+1))
backlink.text = FN_BACKLINK_TEXT
if li.getchildren():
node = li[-1]
if node.tag == "p":
node.text = node.text + NBSP_PLACEHOLDER
node.append(backlink)
else:
p = etree.SubElement(li, "p")
p.append(backlink)
return div
示例5: handleMatch
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import SubElement [as 别名]
def handleMatch(self, m):
id = m.group(2)
if id in list(self.footnotes.footnotes.keys()):
sup = etree.Element("sup")
a = etree.SubElement(sup, "a")
sup.set('id', self.footnotes.makeFootnoteRefId(id))
a.set('href', '#' + self.footnotes.makeFootnoteId(id))
a.set('rel', 'footnote')
a.text = str(self.footnotes.footnotes.index(id) + 1)
return sup
else:
return None
示例6: run
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import SubElement [as 别名]
def run(self, parent, blocks):
""" Parse a table block and build table. """
block = blocks.pop(0).split('\n')
header = block[0].strip()
seperator = block[1].strip()
rows = block[2:]
# Get format type (bordered by pipes or not)
border = False
if header.startswith('|'):
border = True
# Get alignment of columns
align = []
for c in self._split_row(seperator, border):
if c.startswith(':') and c.endswith(':'):
align.append('center')
elif c.startswith(':'):
align.append('left')
elif c.endswith(':'):
align.append('right')
else:
align.append(None)
# Build table
table = etree.SubElement(parent, 'table')
thead = etree.SubElement(table, 'thead')
self._build_row(header, thead, align, border)
tbody = etree.SubElement(table, 'tbody')
for row in rows:
self._build_row(row.strip(), tbody, align, border)
示例7: create_item
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import SubElement [as 别名]
def create_item(self, parent, block):
""" Create a new dd and parse the block with it as the parent. """
dd = markdown.etree.SubElement(parent, 'dd')
self.parser.parseBlocks(dd, [block])
示例8: run
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import SubElement [as 别名]
def run(self, parent, blocks):
diag_blocks = []
for block in blocks:
block = block.strip()
diag_blocks.append(block)
if block.endswith("}"):
break
raw_block = "\n".join(diag_blocks)
del blocks[:len(diag_blocks)]
font_path = self.extension.getConfig('fontpath')
font_antialias = self.extension.getConfig('fontantialias')
output_fmt = self.extension.getConfig('format')
diagram = draw_blockdiag(raw_block, output_fmt=output_fmt, font_path=font_path, font_antialias=font_antialias)
if output_fmt == 'png':
src_data = 'data:image/png;base64,{0}'.format(
base64.b64encode(diagram).decode('ascii')
)
else:
src_data = 'data:image/svg+xml;charset=utf-8,{0}'.format(url_quote(diagram))
p = etree.SubElement(parent, 'p')
img = etree.SubElement(p, 'img')
img.attrib['src'] = src_data
示例9: run
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import SubElement [as 别名]
def run(self, parent, blocks):
"""Convert to details/summary block."""
sibling = self.lastChild(parent)
block = blocks.pop(0)
m = self.START.search(block)
if m:
# remove the first line
block = block[m.end():]
# Get the details block and and the non-details content
block, non_details = self.detab(block)
if m:
state = m.group(1)
is_open = state is not None
if m.group(4):
class_name = self.COMPRESS_SPACES.sub(' ', m.group(4).lower())
title = class_name.split(' ')[0].capitalize()
else:
classes = m.group(2)
class_name = '' if classes is None else self.COMPRESS_SPACES.sub(' ', classes.lower())
title = m.group(3)
div = etree.SubElement(parent, 'details', ({'open': 'open'} if is_open else {}))
if class_name:
div.set('class', class_name)
summary = etree.SubElement(div, 'summary')
summary.text = title
else:
div = sibling
self.parser.parseChunk(div, block)
if non_details:
# Insert the non-details content back into blocks
blocks.insert(0, non_details)
示例10: run
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import SubElement [as 别名]
def run(self, parent, blocks):
""" Parse a table block and build table. """
block = blocks.pop(0).split('\n')
header = block[0].strip()
seperator = block[1].strip()
rows = block[2:]
# Get format type (bordered by pipes or not)
border = False
if header.startswith('|'):
border = True
# Get alignment of columns
align = []
for c in self._split_row(seperator, border):
if c.startswith(':') and c.endswith(':'):
align.append('center')
elif c.startswith(':'):
align.append('left')
elif c.endswith(':'):
align.append('right')
else:
align.append(None)
# Build table
table = etree.SubElement(parent, 'table')
table.set('class', 'wftable')
thead = etree.SubElement(table, 'thead')
self._build_row(header, thead, align, border)
tbody = etree.SubElement(table, 'tbody')
for row in rows:
self._build_row(row.strip(), tbody, align, border)
示例11: run
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import SubElement [as 别名]
def run (self, root):
rss = etree.Element("rss")
rss.set("version", "2.0")
channel = etree.SubElement(rss, "channel")
for tag, text in (("title", self.ext.getConfig("TITLE")),
("link", self.ext.getConfig("URL")),
("description", None)):
element = etree.SubElement(channel, tag)
element.text = text
for child in root:
if child.tag in ["h1", "h2", "h3", "h4", "h5"]:
heading = child.text.strip()
item = etree.SubElement(channel, "item")
link = etree.SubElement(item, "link")
link.text = self.ext.getConfig("URL")
title = etree.SubElement(item, "title")
title.text = heading
guid = ''.join([x for x in heading if x.isalnum()])
guidElem = etree.SubElement(item, "guid")
guidElem.text = guid
guidElem.set("isPermaLink", "false")
elif child.tag in ["p"]:
try:
description = etree.SubElement(item, "description")
except UnboundLocalError:
# Item not defined - moving on
pass
else:
if len(child):
content = "\n".join([etree.tostring(node)
for node in child])
else:
content = child.text
pholder = self.markdown.htmlStash.store(
"<![CDATA[ %s]]>" % content)
description.text = pholder
return rss
示例12: run
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import SubElement [as 别名]
def run(self, fname, optstr):
opts = {}
opts['spec'] = 'width-500'
opts['classname'] = 'left'
for opt in optstr:
bits = opt.split('=', 1)
opt = bits[0]
value = ''
if len(bits) > 1:
value = bits[1]
if opt == 'left':
opts['classname'] = 'left'
elif opt == 'right':
opts['classname'] = 'right'
elif opt == 'full':
opts['classname'] = 'full-width'
elif opt == 'width':
try:
opts['spec'] = "width-%d" % int(value)
except ValueError:
pass
try:
image = get_image_model().objects.get(title=fname)
except ObjectDoesNotExist:
return '[image "{}" not found]'.format(fname)
except MultipleObjectsReturned:
return '[multiple images "{}" found]'.format(fname)
image_url = image.file.url
rendition = image.get_rendition(opts['spec'])
a = etree.Element('a')
a.set('data-toggle', 'lightbox')
a.set('data-type', 'image')
a.set('href', image_url)
img = etree.SubElement(a, 'img')
img.set('src', rendition.url)
img.set('class', opts['classname'])
img.set('width', str(rendition.width))
img.set('height', str(rendition.height))
return a