Skip to content
This repository has been archived by the owner on Nov 18, 2024. It is now read-only.

Commit

Permalink
Vendor the updated mdx_attr_cols from CTPUG/mdx_attr_cols#6.
Browse files Browse the repository at this point in the history
  • Loading branch information
hodgestar committed Apr 25, 2023
1 parent 75ff581 commit bc4f2cc
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions mdx_attr_cols.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
""" Bootstrap 3 grid extension fo Python Markdown
"""

from xml.etree import ElementTree as etree

from markdown import Extension
from markdown.treeprocessors import Treeprocessor


class AttrColTreeProcessor(Treeprocessor):
""" The attr_cols element tree processor. """

CONF_DEFAULTS = {
'columns': 12,
'attr': 'cols',
'tags': ['section'],
}

def __init__(self, md, conf):
super(AttrColTreeProcessor, self).__init__(md)
conf_vars = self.CONF_DEFAULTS.copy()
conf_vars.update(conf or {})
self.columns = int(conf_vars.get('columns'))
self.attr = str(conf_vars.get('attr'))
self.tags = set(conf_vars.get('tags'))

def create_rows(self, node):
row, row_cols = None, 0
for i, child in enumerate(list(node)):
if child.tag.lower() not in self.tags:
continue

if self.attr not in child.attrib:
if row is None:
# if there currently isn't a row, search sub-sections
self.create_rows(child)
else:
row, row_cols = None, 0
continue
cols = int(child.attrib[self.attr])
del child.attrib[self.attr]

if row is None:
row = etree.Element("div", {"class": "row"})
node.insert(i, row)

col = etree.SubElement(row, "div", {
"class": "col-md-%d" % (cols,),
})
row_cols += cols

col.append(child)
node.remove(child)

if row_cols >= self.columns:
row, row_cols = None, 0

def run(self, root):
self.create_rows(root)
return root


class AttrColExtension(Extension):
""" The attr_cols markdown extension. """

REQUIRED_EXTENSIONS = ('attr_list', 'mdx_outline')
REQUIRED_EXTENSION_INTERNAL_NAMES = ('attr_list', 'outline')

def __init__(self, configs):
self.conf = configs

def extendMarkdown(self, md, md_globals=None):
"""Initializes markdown extension components."""
if any(
x not in md.treeprocessors
for x in self.REQUIRED_EXTENSION_INTERNAL_NAMES):
raise RuntimeError(
"The attr_cols markdown extension depends the following"
" extensions which must preceded it in the extension"
" list: %s" % ", ".join(self.REQUIRED_EXTENSIONS))
processor = AttrColTreeProcessor(md, self.conf)
md.treeprocessors.register(
processor, 'attr_cols',
5) # 5 is the lowest priority


def makeExtension(**kwargs):
""" Initialize the attr_cols extension. """
return AttrColExtension(configs=kwargs)

0 comments on commit bc4f2cc

Please sign in to comment.