我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用markdown.util()。
def resolve_url(self, url): """ Resolve internal URLs. Returns None if the URL does not need changing, else returns the new URL. """ from markdown.util import AMP_SUBSTITUTE if not url: return None if url.startswith(AMP_SUBSTITUTE): # Possibly an overencoded mailto: link. # see https://bugs.debian.org/816218 # # Markdown then further escapes & with utils.AMP_SUBSTITUTE, so # we look for it here. return None parsed = urlparse(url) if parsed.scheme or parsed.netloc: return None if not parsed.path: return None dest = self.page.resolve_link(parsed.path) # Also allow .md extension in if dest is None and parsed.path.endswith(".md"): dirname, basename = os.path.split(parsed.path) if basename in ("index.md", "README.md"): dest = self.page.resolve_link(dirname) else: dest = self.page.resolve_link(parsed.path[:-3]) if dest is None: log.warn("%s: internal link %r does not resolve to any site page", self.page.src_relpath, url) return None return urlunparse( (parsed.scheme, parsed.netloc, dest.dst_link, parsed.params, parsed.query, parsed.fragment) )
def run(self, parent, blocks): sibling = self.lastChild(parent) block = blocks.pop(0) block, theRest = self.detab(block) block = block.rstrip() block_is_html = False if "<div " in block or "</" in block or "<span " in block: block_is_html = True if (sibling is not None and sibling.tag == "div"): # The previous block was a code block. As blank lines do not start # new code blocks, append this block to the previous, adding back # linebreaks removed from the split into a list. block_is_html = block_is_html and not isinstance(sibling.text, AtomicString) block = u'\n'.join([sibling.text, block]) output = sibling else: # This is a new codeblock. Create the elements and insert text. output = markdown.util.etree.SubElement(parent, 'div', {'class': 'code-output'}) # If not HTML, add the `pre` class so that we know to render output as raw text if not block_is_html and 'pre' not in output.get('class', 'code-output'): output.set('class', ' '.join([output.get('class', ''), 'pre'])) output.text = "{}\n".format(block) if block_is_html else AtomicString("{}\n".format(block)) if theRest: # This block contained unindented line(s) after the first indented # line. Insert these lines as the first block of the master blocks # list for future processing. blocks.insert(0, theRest)
def handleMatch(self, m): node = markdown.util.etree.Element('mathjax') node.text = markdown.util.AtomicString(m.group(2) + m.group(3) + m.group(2)) return node