Skip to content

Commit

Permalink
fixed a few bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
bboc committed Feb 10, 2024
1 parent 5fb786e commit bd6150d
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions mdbuild/renderer/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
from functools import partial
import logging
import markdown

from .common import HEADLINE_PATTERN

Expand All @@ -12,15 +13,17 @@
class MetadataFilter(object):
"""Process stream and extract/process metadata (title, summary etc.)"""

# extracted metadata
# extracted metadata (acessed from outside)
title = None
summary = None
metadata = None
summary_lines = None

# store next filter funktion to use
filter_function = None
# store next filter function to use
_filter_function = None

# internal buffer for summary
_summary_lines = None

METADATA_PATTERN = re.compile(r'\[\:(?P<key>.*?)\]: # \"(?P<value>.*?)\"')
YAML_METADATA_PATTERN = re.compile(r'(?P<key>.*?):\w+\"(?P<value>.*?)\"')

Expand All @@ -32,7 +35,7 @@ class MetadataFilter(object):
SUMMARY_MARKUP = {
'html': {
BEGIN_SUMMARY: '<div class="card summary"><div class="card-body">',
END_SUMMARY: '</div></div>',
END_SUMMARY: '</div></div>\n',
},
'epub': {
BEGIN_SUMMARY: '<p class="summary">',
Expand All @@ -44,7 +47,7 @@ class MetadataFilter(object):
},
None: {
BEGIN_SUMMARY: None,
END_SUMMARY: None,
END_SUMMARY: '\n',
},
'preserve': {
BEGIN_SUMMARY: BEGIN_SUMMARY,
Expand All @@ -67,7 +70,7 @@ def _header_filter(cls, line, after_metadata=False):
key = match.groupdict()['key']
value = match.groupdict()['value']
cls.metadata[key] = value
cls.filter_function = cls._header_filter
cls._filter_function = cls._header_filter
return None

elif line.strip().startswith('#'):
Expand All @@ -78,17 +81,17 @@ def _header_filter(cls, line, after_metadata=False):
except AttributeError:
logger.warning("title not set")
cls.title = ''
cls.filter_function = cls._standard_filter
cls._filter_function = cls._standard_filter
return line

elif line.strip() == '':
# process empty line
if after_metadata:
cls.filter_function = cls._standard_filter
cls._filter_function = cls._standard_filter
return line
else:
# ignore one blank line
cls.filter_function = partial(cls._header_filter, after_metadata=True)
cls._filter_function = partial(cls._header_filter, after_metadata=True)
return None
else:
raise Exception('Metadata must be followed by an empty line!')
Expand All @@ -102,26 +105,30 @@ def _summary_filter(cls, line):
Transition to standard filter after end of summary.
"""
if line.strip() == cls.END_SUMMARY:
cls.filter_function = cls._standard_filter
cls._filter_function = cls._standard_filter
return cls.SUMMARY_MARKUP[cls.target_format][cls.END_SUMMARY]
else:
# remove bold around summary if present
if line.startswith("**") or line.startswith("__"):
sline = line.strip()[2:-2]
else:
sline = line
cls.summary_lines.append(sline)
cls.summary = '\n'.join(cls.summary_lines)
sline = line.strip()
cls._summary_lines.append(sline)
cls.summary = '\n'.join(cls._summary_lines)

if cls.target_format == 'latex':
# wrap summary in bold for latex (for now)
# TODO: add LaTeX markup for a proper box or something nice
if line.startswith("**") or line.startswith("__"):
pass
else:
line = "**%s**" % line
cls.filter_function = cls._summary_filter
return line
line = "**%s**\n\n" % line.strip()
cls._filter_function = cls._summary_filter
if cls.target_format == "html":
# render to markdown (and strip <p>)
return markdown.markdown(line)[3:-4] + "\n"
else:
return line

@classmethod
def _standard_filter(cls, line):
Expand All @@ -131,10 +138,10 @@ def _standard_filter(cls, line):
Transition to summary filter on encountering summary tag.
"""
if line.strip() == cls.BEGIN_SUMMARY:
cls.filter_function = cls._summary_filter
cls._filter_function = cls._summary_filter
return cls.SUMMARY_MARKUP[cls.target_format][cls.BEGIN_SUMMARY]
else:
cls.filter_function = cls._standard_filter
cls._filter_function = cls._standard_filter
return line

@classmethod
Expand Down Expand Up @@ -169,15 +176,15 @@ def filter(cls, lines, target_format=None):
# Initialize all class variables
cls.title = None
cls.summary = None
cls.summary_lines = []
cls._summary_lines = []
cls.metadata = {}
cls.target_format = target_format

if target_format not in cls.SUMMARY_MARKUP:
raise Exception("Error: unknown target_format '%s'" % target_format)

cls.filter_function = cls._header_filter
cls._filter_function = cls._header_filter
for line in lines:
res = cls.filter_function(line)
res = cls._filter_function(line)
if res is not None:
yield res

0 comments on commit bd6150d

Please sign in to comment.