Example #1
0
 def run(self, parent, blocks):
     block = blocks.pop(0)
     m = self.RE.search(block)
     if m:
         before = block[:m.start()]  # All lines before header
         after = block[m.end():]  # All lines after header
         if before:
             # As the header was not the first line of the block and the
             # lines before the header must be parsed first,
             # recursively parse this lines as a block.
             self.parser.parseBlocks(parent, [before])
         # Create header using named groups from RE
         start_level, force_id = self._get_meta()
         level = len(m.group('level')) + start_level
         if level > 6:
             level = 6
         h = etree.SubElement(parent, 'h%d' % level)
         h.text = m.group('header').strip()
         if m.group('id'):
             h.set('id', self._unique_id(m.group('id')))
         elif force_id:
             h.set('id', self._create_id(m.group('header').strip()))
         if after:
             # Insert remaining lines as first block for future parsing.
             blocks.insert(0, after)
     else:
         # This should never happen, but just in case...
         message(CRITICAL, "We've got a problem header!")
Example #2
0
 def run(self, parent, blocks):
     block = blocks.pop(0)
     m = self.RE.search(block)
     if m:
         before = block[: m.start()]  # All lines before header
         after = block[m.end() :]  # All lines after header
         if before:
             # As the header was not the first line of the block and the
             # lines before the header must be parsed first,
             # recursively parse this lines as a block.
             self.parser.parseBlocks(parent, [before])
         # Create header using named groups from RE
         start_level, force_id = self._get_meta()
         level = len(m.group("level")) + start_level
         if level > 6:
             level = 6
         h = etree.SubElement(parent, "h%d" % level)
         h.text = m.group("header").strip()
         if m.group("id"):
             h.set("id", self._unique_id(m.group("id")))
         elif force_id:
             h.set("id", self._create_id(m.group("header").strip()))
         if after:
             # Insert remaining lines as first block for future parsing.
             blocks.insert(0, after)
     else:
         # This should never happen, but just in case...
         message(CRITICAL, "We've got a problem header!")
Example #3
0
def load_extension(ext_name, configs=[]):
    """Load extension by name, then return the module.

    The extension name may contain arguments as part of the string in the
    following format: "extname(key1=value1,key2=value2)"

    """

    # Parse extensions config params (ignore the order)
    configs = dict(configs)
    pos = ext_name.find("(")  # find the first "("
    if pos > 0:
        ext_args = ext_name[pos + 1:-1]
        ext_name = ext_name[:pos]
        pairs = [x.split("=") for x in ext_args.split(",")]
        configs.update([(x.strip(), y.strip()) for (x, y) in pairs])

    # Setup the module names
    ext_module = 'markdown.extensions'
    module_name_new_style = '.'.join([ext_module, ext_name])
    module_name_old_style = '_'.join(['mdx', ext_name])

    # Try loading the extention first from one place, then another
    try:  # New style (markdown.extensons.<extension>)
        module = __import__(module_name_new_style, {}, {}, [ext_module])
    except ImportError:
        try:  # Old style (mdx.<extension>)
            module = __import__(module_name_old_style)
        except ImportError:
            message(
                WARN, "Failed loading extension '%s' from '%s' or '%s'" %
                (ext_name, module_name_new_style, module_name_old_style))
            # Return None so we don't try to initiate none-existant extension
            return None

    # If the module is loaded successfully, we expect it to define a
    # function called makeExtension()
    try:
        return module.makeExtension(configs.items())
    except AttributeError, e:
        message(CRITICAL,
                "Failed to initiate extension '%s': %s" % (ext_name, e))
Example #4
0
def load_extension(ext_name, configs = []):
    """Load extension by name, then return the module.

    The extension name may contain arguments as part of the string in the
    following format: "extname(key1=value1,key2=value2)"

    """

    # Parse extensions config params (ignore the order)
    configs = dict(configs)
    pos = ext_name.find("(") # find the first "("
    if pos > 0:
        ext_args = ext_name[pos+1:-1]
        ext_name = ext_name[:pos]
        pairs = [x.split("=") for x in ext_args.split(",")]
        configs.update([(x.strip(), y.strip()) for (x, y) in pairs])

    # Setup the module names
    ext_module = 'markdown.extensions'
    module_name_new_style = '.'.join([ext_module, ext_name])
    module_name_old_style = '_'.join(['mdx', ext_name])

    # Try loading the extention first from one place, then another
    try: # New style (markdown.extensons.<extension>)
        module = __import__(module_name_new_style, {}, {}, [ext_module])
    except ImportError:
        try: # Old style (mdx.<extension>)
            module = __import__(module_name_old_style)
        except ImportError:
           message(WARN, "Failed loading extension '%s' from '%s' or '%s'"
               % (ext_name, module_name_new_style, module_name_old_style))
           # Return None so we don't try to initiate none-existant extension
           return None

    # If the module is loaded successfully, we expect it to define a
    # function called makeExtension()
    try:
        return module.makeExtension(configs.items())
    except AttributeError, e:
        message(CRITICAL, "Failed to initiate extension '%s': %s" % (ext_name, e))