本文整理汇总了Python中markdown.util.etree.Element方法的典型用法代码示例。如果您正苦于以下问题:Python etree.Element方法的具体用法?Python etree.Element怎么用?Python etree.Element使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类markdown.util.etree
的用法示例。
在下文中一共展示了etree.Element方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handleMatch
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import Element [as 别名]
def handleMatch(self, match):
reply_id = int(match.group(3))
try:
link = etree.Element("a")
link.attrib["href"] = url_for_post(reply_id)
link.attrib["class"] = "post-reply"
link.attrib["data-post-id"] = str(reply_id)
link.attrib["data-toggle"] = "tooltip"
link.attrib["data-placement"] = "bottom"
link.attrib["data-html"] = "true"
link.attrib["title"] = "<i>Loading...</i>"
link.attrib["data-loaded"] = "false"
link.text = ">>%s" % reply_id
return link
except NoResultFound:
dead_reply = etree.Element("span")
dead_reply.text = ">>%s" % reply_id
dead_reply.attrib["class"] = "text-danger"
return dead_reply
示例2: handleMatch
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import Element [as 别名]
def handleMatch(self, m):
# Extract information from markdown tag
text = m.group(2)
ids = [
id
for id in m.group(3).split(',')
if id.startswith('#')
]
id = ids[0] if ids else None
class_names = [
class_name[1:] if class_name.startswith('.') else class_name
for class_name in m.group(3).split(' ')
if class_name.startswith('.')
]
# Generate HTML element for new span
el = etree.Element('span')
el.text = text
if id:
el.attrib['id'] = id
if class_names:
el.attrib['class'] = " ".join(class_names)
return el
示例3: run
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import Element [as 别名]
def run(self, root):
parent_map = {c: p for p in root.iter() for c in p}
images = root.getiterator("img")
for image in images:
desc = image.attrib["alt"]
if self.re.match(desc):
image.set("alt", desc)
parent = parent_map[image]
ix = list(parent).index(image)
div_node = etree.Element('div')
div_node.set("class", "lightgallery")
new_node = etree.Element('a')
new_node.set("href", image.attrib["src"])
new_node.append(image)
div_node.append(new_node)
parent.insert(ix, div_node)
parent.remove(image)
示例4: run
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import Element [as 别名]
def run(self, name, optstr):
try:
text = name
if optstr:
text = optstr[0]
page = Page.objects.get(title=name)
url = page.url
a = etree.Element('a')
a.set('href', url)
a.text = text
return a
except ObjectDoesNotExist:
return '[page "{}" not found]'.format(name)
except MultipleObjectsReturned:
return '[multiple pages "{}" found]'.format(name)
示例5: run
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import Element [as 别名]
def run(self, name, optstr):
try:
text = name
if len(optstr):
text = optstr[0]
doc = Document.objects.get(title=name)
url = doc.url
a = etree.Element('a')
a.set('href', url)
a.text = text
return a
except ObjectDoesNotExist:
return '[document "{}" not found]'.format(name)
except MultipleObjectsReturned:
return '[multiple documents "{}" found]'.format(name)
示例6: handleMatch
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import Element [as 别名]
def handleMatch(self, m):
abbr = etree.Element('abbr')
abbr.text = m.group('abbr')
abbr.set('title', self.title)
return abbr
示例7: findFootnotesPlaceholder
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import Element [as 别名]
def findFootnotesPlaceholder(self, root):
""" Return ElementTree Element that contains Footnote placeholder. """
def finder(element):
for child in element:
if child.text:
if child.text.find(self.getConfig("PLACE_MARKER")) > -1:
return child, element, True
if child.tail:
if child.tail.find(self.getConfig("PLACE_MARKER")) > -1:
return child, element, False
finder(child)
return None
res = finder(root)
return res
示例8: makeFootnotesDiv
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import Element [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
示例9: handleMatch
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import Element [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
示例10: handleMatch
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import Element [as 别名]
def handleMatch(self, m):
supr = m.group(3)
text = supr
el = etree.Element("sup")
el.text = AtomicString(text)
return el
示例11: handleMatch
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import Element [as 别名]
def handleMatch(self, m):
subsc = m.group(3)
text = subsc
el = etree.Element("sub")
el.text = AtomicString(text)
return el
示例12: handleMatch
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import Element [as 别名]
def handleMatch(self, match):
return etree.Element("br")
示例13: handleMatch
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import Element [as 别名]
def handleMatch(self, match):
spoiler = etree.Element("span")
spoiler.text = match.group(2)
spoiler.attrib["class"] = "spoiler"
return spoiler
示例14: insert_permalinks
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import Element [as 别名]
def insert_permalinks(el, permalink_text=None, permalink_class=None, permalink_title=None):
for child in el:
if child.tag in {'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8'}:
permalink = etree.Element('a')
# try to get any existing ID from the heading tag
id = child.get('id', None)
# if there is none, slugify the text of the heading tag
if id is None:
heading_text = strip_el_text(child, max_depth=3)
id = slugify(heading_text.replace('\'', '').replace('"', ''))
# apply the ID to the heading
child.set('id', id)
permalink.set('href', '#%s' % id)
if permalink_class is not None:
permalink.set('class', permalink_class)
if permalink_title is not None:
permalink.set('title', permalink_title)
if permalink_text is not None:
permalink.text = permalink_text
child.append(permalink)
# recursively insert permalinks
insert_permalinks(
child,
permalink_text=permalink_text,
permalink_class=permalink_class,
permalink_title=permalink_title
)
示例15: run
# 需要导入模块: from markdown.util import etree [as 别名]
# 或者: from markdown.util.etree import Element [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