diff --git a/.gitignore b/.gitignore index 1994c11..e36b4de 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,133 @@ -*.egg-info -*.pyc -*.swp +/srcdocs/wiki + +# Mac OS X internals +*.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon + + +# Thumbnails +._* + +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent + +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + +# Bower and NPM libraries +bower_components +node_modules + +# Build files +build +MANIFEST +site + +# PyCharm CE files +.idea/ + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*,cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# dotenv +.env + +# virtualenv +.venv/ +venv/ +ENV/ + +# Spyder project settings +.spyderproject + +# Rope project settings +.ropeproject diff --git a/.idea/dbnavigator.xml b/.idea/dbnavigator.xml new file mode 100644 index 0000000..792a196 --- /dev/null +++ b/.idea/dbnavigator.xml @@ -0,0 +1,445 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..280893b --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,15 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..9eafce5 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/mkdocs-pandoc.iml b/.idea/mkdocs-pandoc.iml new file mode 100644 index 0000000..6f63a63 --- /dev/null +++ b/.idea/mkdocs-pandoc.iml @@ -0,0 +1,12 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..4f94860 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..b90305e --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,442 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Python + + + + + PyCompatibilityInspection + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/CHANGES b/CHANGES index 84de39e..a026261 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,7 @@ +0.2.6.3: + + * Added support for pages without titles specified in mkdocs.yml + 0.2.6: * Fixed issues/11 (added support for underwide header rows in tables) diff --git a/mkdocs_pandoc/cli/mkdocs2pandoc.py b/mkdocs_pandoc/cli/mkdocs2pandoc.py index 5cb12a2..78f474c 100644 --- a/mkdocs_pandoc/cli/mkdocs2pandoc.py +++ b/mkdocs_pandoc/cli/mkdocs2pandoc.py @@ -29,14 +29,14 @@ def main(): opts = argparse.ArgumentParser( - description="mdtableconv.py " + - "- converts pipe delimited tables to Pandoc's grid tables") + description="mkdocs2pandoc.py " + + "- flattens an MkDocs source site into a single Markdown document") opts.add_argument('-e', '--encoding', default='utf-8', help="Set encoding for input files (default: utf-8)") opts.add_argument('-f', '--config-file', default='mkdocs.yml', - help="mkdocs configuration file to use") + help="MkDocs configuration file to use") opts.add_argument('-i', '--image-ext', default=None, help="Extension to substitute image extensions by (default: no replacement)") @@ -50,6 +50,8 @@ def main(): opts.add_argument('-o', '--outfile', default=None, help="File to write finished pandoc document to (default: STDOUT)") + opts.add_argument('-v', '--version', action='version', version='%(prog)s 0.2.7') + args = opts.parse_args() # Python 2 and Python 3 have mutually incompatible approaches to writing diff --git a/mkdocs_pandoc/filters/headlevels.py b/mkdocs_pandoc/filters/headlevels.py index 64a4728..ff07263 100644 --- a/mkdocs_pandoc/filters/headlevels.py +++ b/mkdocs_pandoc/filters/headlevels.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. # - import re # TODO: Implement handling for Setext style headers. @@ -26,7 +25,7 @@ def __init__(self, pages): # Determine maximum header level from nesting in mkdocs.yml for page in pages: if page['level'] > max_offset: - max_offset = page['level'] + max_offset = page['level'] - 1 self.offset = max_offset @@ -35,6 +34,6 @@ def run(self, lines): """Filter method""" ret = [] for line in lines: - ret.append(re.sub(r'^#', '#' + ('#' * self.offset), line)) + ret.append(re.sub(r'^#', ('#' * self.offset), line)) return ret 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 7b89a8b..e8b87c4 100644 --- a/mkdocs_pandoc/pandoc_converter.py +++ b/mkdocs_pandoc/pandoc_converter.py @@ -1,12 +1,15 @@ 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 import mkdocs_pandoc.filters.toc import mkdocs_pandoc.filters.xref +import mkdocs.utils from mkdocs_pandoc.exceptions import FatalError @@ -26,6 +29,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: @@ -70,11 +75,18 @@ def flatten_pages(self, pages, level=1): flattened = [] for page in pages: + if type(page) is str: + flattened.append( + { + 'file' : page, + 'title': mkdocs.utils.filename_to_title(page), + 'level': level, + }) if type(page) is list: flattened.append( { 'file': page[0], - 'title': page[1], + 'title': '%s {.unnumbered}' % page[1], 'level': level, }) if type(page) is dict: @@ -82,10 +94,18 @@ def flatten_pages(self, pages, level=1): flattened.append( { 'file': list(page.values())[0], - 'title': list(page.keys())[0], + 'title': '%s {.unnumbered}' % list(page.keys())[0], 'level': level, }) if type(list(page.values())[0]) is list: + # Add the parent section + flattened.append( + { + 'file': None, + 'title': '%s {.unnumbered}' % list(page.keys())[0], + 'level': level, + }) + # Add children sections flattened.extend( self.flatten_pages( list(page.values())[0], @@ -115,12 +135,17 @@ def convert(self): f_headlevel = mkdocs_pandoc.filters.headlevels.HeadlevelFilter(pages) for page in pages: - fname = os.path.join(self.config['docs_dir'], page['file']) - try: - p = codecs.open(fname, 'r', self.encoding) - except IOError as e: - raise FatalError("Couldn't open %s for reading: %s" % (fname, - e.strerror), 1) + lines_tmp = [] + if page['file']: + fname = os.path.join(self.config['docs_dir'], page['file']) + try: + with codecs.open(fname, 'r', self.encoding) as p: + for line in p.readlines(): + lines_tmp.append(line.rstrip()) + except IOError as e: + raise FatalError("Couldn't open %s for reading: %s" % (fname, + e.strerror), 1) + f_chapterhead = mkdocs_pandoc.filters.chapterhead.ChapterheadFilter( headlevel=page['level'], title=page['title'] @@ -131,17 +156,13 @@ def convert(self): image_path=self.config['site_dir'], image_ext=self.image_ext) - lines_tmp = [] - - for line in p.readlines(): - lines_tmp.append(line.rstrip()) - if self.exclude: lines_tmp = f_exclude.run(lines_tmp) 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) @@ -154,6 +175,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) diff --git a/setup.py b/setup.py index 68dcf29..17ae995 100644 --- a/setup.py +++ b/setup.py @@ -17,7 +17,7 @@ setup( name='mkdocs-pandoc', - version='0.2.6', + version='0.2.6.3', description='A translator from mkdocs style markdown to pandoc style ' + 'markdown',