diff --git a/mkdocs_pandoc/filters/math.py b/mkdocs_pandoc/filters/math.py new file mode 100644 index 0000000..e563ef6 --- /dev/null +++ b/mkdocs_pandoc/filters/math.py @@ -0,0 +1,28 @@ +# Copyright 2015 Johannes Grassler +# Copyright 2016 Kergonath +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import re + +class MathFilter(object): + """Turn the \( \) Markdown math notation into LaTex $$ inlines""" + + def run(self, lines): + """Filter method""" + ret = [] + for line in lines: + ret.append(re.sub(r'\\\((.*)\\\)', r'$\1$', line)) + + return ret diff --git a/mkdocs_pandoc/filters/metadata.py b/mkdocs_pandoc/filters/metadata.py new file mode 100644 index 0000000..03a8040 --- /dev/null +++ b/mkdocs_pandoc/filters/metadata.py @@ -0,0 +1,34 @@ +# Copyright 2015 Johannes Grassler +# Copyright 2016 Kergonath +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import re + +class MetadataFilter(object): + """Turn the \( \) Markdown math notation into LaTex $$ inlines""" + + def run(self, lines): + """Filter method""" + ret = [] + header = True + for line in lines: + if header: + if not re.match(r'^[a-zA-Z\ ]:', line): + header = False + ret.append(line) + else: + ret.append(line) + + return ret diff --git a/mkdocs_pandoc/pandoc_converter.py b/mkdocs_pandoc/pandoc_converter.py index 2ba4dbb..2c9189c 100644 --- a/mkdocs_pandoc/pandoc_converter.py +++ b/mkdocs_pandoc/pandoc_converter.py @@ -1,7 +1,9 @@ import mkdocs_pandoc.filters.anchors +import mkdocs_pandoc.filters.math import mkdocs_pandoc.filters.chapterhead import mkdocs_pandoc.filters.headlevels import mkdocs_pandoc.filters.images +import mkdocs_pandoc.filters.metadata import mkdocs_pandoc.filters.exclude import mkdocs_pandoc.filters.include import mkdocs_pandoc.filters.tables @@ -26,6 +28,8 @@ def __init__(self, **kwargs): self.filter_xrefs = kwargs.get('filter_xrefs', True) self.image_ext = kwargs.get('image_ext', None) self.strip_anchors = kwargs.get('strip_anchors', True) + self.strip_metadata = kwargs.get('strip_metadata', True) + self.convert_math = kwargs.get('convert_math', True) self.width = kwargs.get('width', 100) try: @@ -133,7 +137,8 @@ def convert(self): if self.filter_include: lines_tmp = f_include.run(lines_tmp) - + + lines_tmp = mkdocs_pandoc.filters.metadata.MetadataFilter().run(lines_tmp) lines_tmp = f_headlevel.run(lines_tmp) lines_tmp = f_chapterhead.run(lines_tmp) lines_tmp = f_image.run(lines_tmp) @@ -146,6 +151,10 @@ def convert(self): if self.strip_anchors: lines = mkdocs_pandoc.filters.anchors.AnchorFilter().run(lines) + # Convert math expressions + if self.convert_math: + lines = mkdocs_pandoc.filters.math.MathFilter().run(lines) + # Fix cross references if self.filter_xrefs: lines = mkdocs_pandoc.filters.xref.XrefFilter().run(lines)