From 58d96ea769135a090a70eb322e536c544ee29c81 Mon Sep 17 00:00:00 2001 From: Artem Yunusov Date: Fri, 8 Aug 2008 03:43:56 +0500 Subject: ElementTree version check added. --- markdown.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'markdown.py') diff --git a/markdown.py b/markdown.py index df85bab..a39a99c 100755 --- a/markdown.py +++ b/markdown.py @@ -58,10 +58,10 @@ def isstr(s): def importETree(): """ Imports best variant of ElementTree and returns module object """ - + cetree = None try: # Python 2.5+ - import xml.etree.cElementTree as etree + import xml.etree.cElementTree as cetree except ImportError: try: # Python 2.5+ @@ -69,7 +69,7 @@ def importETree(): except ImportError: try: # normal cElementTree install - import cElementTree as etree + import cElementTree as cetree except ImportError: try: # normal ElementTree install @@ -78,6 +78,19 @@ def importETree(): message(CRITICAL, "Failed to import ElementTree from any known place") sys.exit(1) + if cetree: + if cetree.VERSION < "1.0": + message(CRITICAL, + "cElementTree version is too old, 1.0 and upper required") + sys.exit(1) + + etree = cetree + else: + if etree.VERSION < "1.1": + message(CRITICAL, + "ElementTree version is too old, 1.1 and upper required") + sys.exit(1) + return etree etree = importETree() -- cgit v1.2.3 From 3c525ce0feda3656c200130ae75837941815e49c Mon Sep 17 00:00:00 2001 From: Artem Yunusov Date: Sat, 9 Aug 2008 04:35:26 +0500 Subject: Comments cleanup, deleted Markdown._processTree function. --- markdown.py | 108 +++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 60 insertions(+), 48 deletions(-) (limited to 'markdown.py') diff --git a/markdown.py b/markdown.py index a39a99c..f64fd16 100755 --- a/markdown.py +++ b/markdown.py @@ -53,11 +53,11 @@ def message(level, text): logger.log(level, text) def isstr(s): + """ Check if it's string """ return isinstance(s, unicode) or isinstance(s, str) def importETree(): - """ Imports best variant of ElementTree - and returns module object """ + """ Import best variant of ElementTree and return module object """ cetree = None try: # Python 2.5+ @@ -96,6 +96,7 @@ def importETree(): etree = importETree() def indentETree(elem, level=0): + """ Indent ElementTree before serialization """ if level > 1: i = "\n" + (level-1) * " " @@ -187,7 +188,7 @@ def isBlockLevel (tag): def codepoint2name(code): - """ Returns entity definition by code, or code + """ Return entity definition by code, or code if there is no such entity definition""" entity = htmlentitydefs.codepoint2name.get(code) if entity: @@ -196,7 +197,7 @@ def codepoint2name(code): return "%s#%d;" % (AND_SUBSTITUTE, code) def handleAttributes(text, parent): - + """ Handale attributes, e.g {@id=123} """ def attributeCallback(match): parent.set(match.group(1), match.group(2)) @@ -626,7 +627,7 @@ class BacktickPattern (Pattern): class DoubleTagPattern (SimpleTagPattern): """ Return a ElementTree element nested in tag2 nested in tag1. - Usefull for strong emphasis etc. + Useful for strong emphasis etc. """ def handleMatch(self, m): @@ -849,7 +850,7 @@ class Postprocessor: """ Subclasses of Postprocessor should implement a `run` method, which takes a root Element. Method can return another Element, and global - root Element will be replaced, or just mnodify current and return None. + root Element will be replaced, or just modify current and return None. """ pass @@ -870,7 +871,7 @@ class TextPostprocessor: """ Subclasses of TextPostprocessor should implement a `run` method, which takes the html document as a single text string and returns a - (possably modified) string. + (possibly modified) string. """ pass @@ -1059,20 +1060,31 @@ def dequote(string): class InlineStash: def __init__(self): + """ Create a InlineStash. """ self.prefix = INLINE_PLACEHOLDER_PREFIX self.suffix = INLINE_PLACEHOLDER_SUFFIX self._nodes = {} self.phLength = 4 + len(self.prefix) + len(self.suffix) def _genPlaceholder(self, type): - """ Generates placeholder """ + """ Generate a placeholder """ id = "%04d" % len(self._nodes) hash = "%s%s:%s%s" % (self.prefix, type, id, self.suffix) return hash, id def extractId(self, data, index): - """ Extracting id from data string, starting from index """ + """ + Extract id from data string, start from index + + Keyword arguments: + + * data: string + * index: index, from which we start search + + Returns: placeholder id and string index, after + found placeholder + """ endIndex = data.find(self.suffix, index+1) if endIndex == -1: return None, index + 1 @@ -1087,15 +1099,17 @@ class InlineStash: return self._nodes.has_key(id) def get(self, id): - """ Returns node by id """ + """ Return node by id """ return self._nodes.get(id) def add(self, node, type): + """ Add node to stash """ pholder, id = self._genPlaceholder(type) self._nodes[id] = node return pholder def rest(self): + """ Reset instance """ self._nodes = {} """ @@ -1633,7 +1647,7 @@ class Markdown: def _handleInline(self, data, patternIndex=0): """ - Processinf string with inline patterns and replasing it + Process string with inline patterns and replace it with placeholders Keyword arguments: @@ -1641,7 +1655,7 @@ class Markdown: * data: A line of Markdown text * patternIndex: The index of the inlinePattern to start with - Return: String with placeholders. + Returns: String with placeholders. """ startIndex = 0 @@ -1659,19 +1673,17 @@ class Markdown: def _applyInline(self, pattern, data, patternIndex, startIndex=0): """ - Given a pattern name, this function checks if the line - fits the pattern, creates the necessary elements, adds it - to InlineStash, and returns string with placeholders, - instead of ElementTree elements. + Check if the line fits the pattern, create the necessary + elements, add it to InlineStash Keyword arguments: * data: the text to be processed * pattern: the pattern to be checked * patternIndex: index of current pattern - * startIndex: string index, from wich we starting search + * startIndex: string index, from which we starting search - Returns: String with placeholders. + Returns: String with placeholders instead of ElementTree elements. """ match = pattern.getCompiledRegExp().match(data[startIndex:]) leftData = data[:startIndex] @@ -1703,7 +1715,19 @@ class Markdown: pholder, match.groups()[-1]), True, 0 def _processElementText(self, node, subnode, isText=True): + """ + Process placeholders in Element.text or Element.tail + of Elements popped from InlineStash + + Keywords arguments: + + * node: parent node + * subnode: processing node + * isText: bool variable, True - it's text, False - it's tail + Returns: None + + """ if isText: text = subnode.text subnode.text = None @@ -1725,9 +1749,12 @@ class Markdown: def _processPlaceholders(self, data, parent): """ - Processes string with placeholders and generates ElementTree tree. + Process string with placeholders and generate ElementTree tree. + + Keyword arguments: * data: string with placeholders instead of ElementTree elements. + * parent: Element, which contains processing inline data Returns: list with ElementTree elements with applied inline patterns. """ @@ -1796,18 +1823,22 @@ class Markdown: data = "" return result - - def _processTree(self, el): + + + def applyInlinePatterns(self, markdownTree): """ - Processing ElementTree, and applying inline patterns + Iterate over ElementTree, find elements with inline tag, + apply inline patterns and append newly created Elements to tree Keyword arguments: - * el - parent element of ElementTree. + * markdownTree: ElementTree object, representing Markdown tree. Returns: ElementTree object with applied inline patterns. """ - + + el = markdownTree.getroot() + stack = [el] while stack: @@ -1844,32 +1875,15 @@ class Markdown: newChild) currElement.insert(pos, newChild) pos += 1 - - - def applyInlinePatterns(self, markdownTree): - """ - Retrun ElementTree, with applied - inline paterns - - Keyword arguments: - - * markdownTree: ElementTree object, reppresenting Markdown tree. - - Returns: ElementTree object. - """ - - el = markdownTree.getroot() - - self._processTree(el) return markdownTree def markdownToTree(self, source=None): """ - Retrun ElementTree, without applying inline paterns, - all data, that should be processed with - inline patterns included in sections. + Create ElementTree, without applying inline paterns, + all data, include all data, that must be procesed wit inline + patterns in sections. Keyword arguments: @@ -1903,13 +1917,11 @@ class Markdown: markdownTree = self._transform() - return markdownTree - - + return markdownTree def convert (self, source=None): """ - Return the document in XHTML format. + Create the document in XHTML format. Keyword arguments: -- cgit v1.2.3 From fe8a2ff307ca2535e841e546cabfa31ce296d4ef Mon Sep 17 00:00:00 2001 From: Artem Yunusov Date: Sat, 9 Aug 2008 04:43:55 +0500 Subject: AND_SUBSTITUTE -> AMP_SUBSTITUTE --- markdown.py | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'markdown.py') diff --git a/markdown.py b/markdown.py index f64fd16..707b89c 100755 --- a/markdown.py +++ b/markdown.py @@ -15,8 +15,8 @@ script. (You might want to read that before you try modifying this file.) Started by [Manfred Stienstra](http://www.dwerg.net/). Continued and -maintained by [Yuri Takhteyev](http://www.freewisdom.org) and [Waylan -Limberg](http://achinghead.com/). +maintained by [Yuri Takhteyev](http://www.freewisdom.org), [Waylan +Limberg](http://achinghead.com/) and [Artem Yunusov](http://blog.splyer.com). Contact: @@ -93,7 +93,10 @@ def importETree(): return etree -etree = importETree() +"""ElementTree module +in extensions use: `from markdown import etree` +to access to the ElemetTree module, do not import it by yourself""" +etree = importETree() def indentETree(elem, level=0): """ Indent ElementTree before serialization """ @@ -162,7 +165,6 @@ EXECUTABLE_NAME_FOR_USAGE = "python markdown.py" # --------------- CONSTANTS YOU _SHOULD NOT_ HAVE TO CHANGE ---------- -AND_SUBSTITUTE = unichr(2) + unichr(4) + unichr(3) # placeholders STX = u'\u0002' # Use STX ("Start of text") for start-of-placeholder @@ -172,6 +174,8 @@ HTML_PLACEHOLDER = HTML_PLACEHOLDER_PREFIX + "%d"+ETX INLINE_PLACEHOLDER_PREFIX = STX+"inline:" INLINE_PLACEHOLDER_SUFFIX = ETX +AMP_SUBSTITUTE = STX+"amp"+ETX + BLOCK_LEVEL_ELEMENTS = ['p', 'div', 'blockquote', 'pre', 'table', 'dl', 'ol', 'ul', 'script', 'noscript', @@ -188,13 +192,15 @@ def isBlockLevel (tag): def codepoint2name(code): - """ Return entity definition by code, or code - if there is no such entity definition""" + """ + Return entity definition by code, or code + if there is no such entity definition + """ entity = htmlentitydefs.codepoint2name.get(code) if entity: - return "%s%s;" % (AND_SUBSTITUTE, entity) + return "%s%s;" % (AMP_SUBSTITUTE, entity) else: - return "%s#%d;" % (AND_SUBSTITUTE, code) + return "%s#%d;" % (AMP_SUBSTITUTE, code) def handleAttributes(text, parent): """ Handale attributes, e.g {@id=123} """ @@ -788,7 +794,7 @@ class AutomailPattern (Pattern): el.text += codepoint2name(ord(letter)) mailto = "mailto:" + email - mailto = "".join([AND_SUBSTITUTE + '#%d;' % + mailto = "".join([AMP_SUBSTITUTE + '#%d;' % ord(letter) for letter in mailto]) el.set('href', mailto) return el @@ -917,10 +923,10 @@ class AndSubstitutePostprocessor(TextPostprocessor): def run(self, text): - text = text.replace(AND_SUBSTITUTE, "&") + text = text.replace(AMP_SUBSTITUTE, "&") return text -ANDSUBSTITUTETEXTPOSTPROCESSOR = AndSubstitutePostprocessor() +AMPSUBSTITUTETEXTPOSTPROCESSOR = AndSubstitutePostprocessor() """ @@ -1203,7 +1209,7 @@ class Markdown: self.textPostprocessors = [# a footnote postprocessor will get # inserted here RAWHTMLTEXTPOSTPROCESSOR, - ANDSUBSTITUTETEXTPOSTPROCESSOR] + AMPSUBSTITUTETEXTPOSTPROCESSOR] self.prePatterns = [] -- cgit v1.2.3 From 2b7e391fcd51d3468133f628f2e013574cf16536 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Sat, 9 Aug 2008 22:55:44 -0400 Subject: reorganized the extensions into a seperate dir. Much cleaner looking file system IMO. --- markdown.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'markdown.py') diff --git a/markdown.py b/markdown.py index 707b89c..38923ac 100755 --- a/markdown.py +++ b/markdown.py @@ -2135,11 +2135,14 @@ def load_extension(ext_name, configs = []): module = __import__(extension_module_name) except ImportError: - message(WARN, + try: + module = __import__('.'.join(['mdx', extension_module_name]), {}, {}, ['mdx']) + except: + message(WARN, "Couldn't load extension '%s' from \"%s\" - continuing without." % (ext_name, extension_module_name) ) - # Return a dummy (do nothing) Extension as silent failure - return Extension(configs={}) + # Return a dummy (do nothing) Extension as silent failure + return Extension(configs={}) return module.makeExtension(configs.items()) -- cgit v1.2.3 From f9bc5d2b86a94fb98e791966bb26e1abd5ef0ed2 Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Mon, 11 Aug 2008 20:12:28 -0400 Subject: renamed extension module and set import to extension module first, then mdx_filename --- markdown.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'markdown.py') diff --git a/markdown.py b/markdown.py index 38923ac..af611c8 100755 --- a/markdown.py +++ b/markdown.py @@ -2129,18 +2129,20 @@ def load_extension(ext_name, configs = []): pairs = [x.split("=") for x in ext_args.split(",")] configs.update([(x.strip(), y.strip()) for (x, y) in pairs]) - extension_module_name = "mdx_" + ext_name + ext_module = 'markdown_extensions' + module_name = '.'.join([ext_module, ext_name]) + extension_module_name = '_'.join(['mdx', ext_name]) try: - module = __import__(extension_module_name) - + module = __import__(module_name, {}, {}, [ext_module]) except ImportError: try: - module = __import__('.'.join(['mdx', extension_module_name]), {}, {}, ['mdx']) + module = __import__(extension_module_name) except: message(WARN, - "Couldn't load extension '%s' from \"%s\" - continuing without." - % (ext_name, extension_module_name) ) + "Failed loading extension '%s' from '%s' or '%s' " + "- continuing without." + % (ext_name, module_name, extension_module_name) ) # Return a dummy (do nothing) Extension as silent failure return Extension(configs={}) -- cgit v1.2.3 From ac578bc2e55183ff0d6d9002ccfdb82d38778a2b Mon Sep 17 00:00:00 2001 From: Waylan Limberg Date: Tue, 12 Aug 2008 13:54:25 -0400 Subject: Removed depreciated 'source' keyword argument from Markdown.__init__() in preparation for 2.0. This should be the last of any depreciated features. --- markdown.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) (limited to 'markdown.py') diff --git a/markdown.py b/markdown.py index af611c8..f0bc0e8 100755 --- a/markdown.py +++ b/markdown.py @@ -1166,7 +1166,7 @@ class Markdown: """ - def __init__(self, source=None, # depreciated + def __init__(self, extensions=[], extension_configs={}, safe_mode = False): @@ -1175,7 +1175,6 @@ class Markdown: Keyword arguments: - * source: The text in Markdown format. Depreciated! * extensions: A list of extensions. If they are of type string, the module mdx_name.py will be loaded. If they are a subclass of markdown.Extension, they will be used @@ -1185,9 +1184,7 @@ class Markdown: """ - self.source = source - if source is not None: - message(WARN, "The `source` arg of Markdown.__init__() is depreciated and will be removed in the future. Use `instance.convert(source)` instead.") + self.source = None self.safeMode = safe_mode self.blockGuru = BlockGuru() self.registeredExtensions = [] -- cgit v1.2.3