Skip to content

Commit

Permalink
Add some useful debug prints
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Flynn <[email protected]>
  • Loading branch information
evan-flynn-apexai committed Aug 11, 2023
1 parent 8ae73a0 commit b748e8f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
15 changes: 9 additions & 6 deletions markdown/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def convert(self, source):
5. The output is written to a string.
"""

logger.debug("Converting markdown file to serialzed XHTML or HTML")
# Fix up the source text
if not source.strip():
return '' # a blank Unicode string
Expand All @@ -246,20 +246,23 @@ def convert(self, source):
raise

# Split into lines and run the line preprocessors.
logger.debug("Split document into lines")
self.lines = source.split("\n")
for prep in self.preprocessors:
logger.debug(f"Running preprocessor prep for {prep.__module__.__str__()}")
self.lines = prep.run(self.lines)

# Parse the high-level elements.
logger.debug("Parse the high-level elements of the file")
root = self.parser.parseDocument(self.lines).getroot()

# Run the tree-processors
logger.debug("Run the tree-processors")
for treeprocessor in self.treeprocessors:
logger.debug(f"Running treeprocessor for {treeprocessor.__module__.__str__()}:{treeprocessor.__class__.__name__}")
newRoot = treeprocessor.run(root)
if newRoot is not None:
root = newRoot

# Serialize _properly_. Strip top-level tags.
logger.debug("Serialize _properly_. Strip top-level tags.")
output = self.serializer(root)
if self.stripTopLevelTags:
try:
Expand All @@ -269,14 +272,14 @@ def convert(self, source):
output = output[start:end].strip()
except ValueError as e: # pragma: no cover
if output.strip().endswith('<%s />' % self.doc_tag):
# We have an empty document
logger.debug("Document is empty")
output = ''
else:
# We have a serious problem
raise ValueError('Markdown failed to strip top-level '
'tags. Document=%r' % output.strip()) from e

# Run the text post-processors
logger.debug("Run the text post-processors")
for pp in self.postprocessors:
output = pp.run(output)

Expand Down
8 changes: 6 additions & 2 deletions markdown/treeprocessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@
License: BSD (see LICENSE.md for details).
"""

import logging
import re
import xml.etree.ElementTree as etree
from . import util
from . import inlinepatterns

logger = logging.getLogger('MARKDOWN')


def build_treeprocessors(md, **kwargs):
""" Build the default `treeprocessors` for Markdown. """
Expand Down Expand Up @@ -340,6 +342,7 @@ def run(self, tree, ancestors=None):
Returns: `ElementTree` object with applied inline patterns.
"""
logger.debug("Running inline treeprocessor")
self.stashed_nodes = {}

# Ensure a valid parent list, but copy passed in lists
Expand Down Expand Up @@ -413,7 +416,7 @@ def _prettifyETree(self, elem):

def run(self, root):
""" Add line breaks to `ElementTree` root object. """

logger.debug("Running line break treeprocessor")
self._prettifyETree(root)
# Do `<br />`'s separately as they are often in the middle of
# inline content and missed by `_prettifyETree`.
Expand Down Expand Up @@ -445,6 +448,7 @@ def unescape(self, text):
return self.RE.sub(self._unescape, text)

def run(self, root):
logger.debug("Running UnescapeTreeprocessor")
""" Loop over all elements and unescape all text. """
for elem in root.iter():
# Unescape text content
Expand Down

0 comments on commit b748e8f

Please sign in to comment.