项目作者: darrida

项目描述 :
GitLab Markdown Wiki Table of Contents
高级语言: Python
项目地址: git://github.com/darrida/md_file_tree.py.git
创建时间: 2019-12-05T15:57:28Z
项目社区:https://github.com/darrida/md_file_tree.py

开源协议:

下载


GitLab Adaptation of md_file_tree.py

**Note: This is a modification of https://github.com/amaiorano/md-to-toc. All credit for the vast majority of this script goes there.

Justification for Changes

  • Goal: Use this script to create a link based TOC index on the Home page of a GitLab Wiki
  • This script gets really close to what I was looking (98% of the hard work)
  • 3 items in the way this script formats the TOC conflicts with what GitLab Wiki pages require
    • (1) The file path needed a ‘.’ at the beginning rather than ‘.\‘
    • (2) The directory separators in the file link needed to be ‘/‘ rather than ‘\‘
    • (3) The file name in the path needed to be ‘Name” rather than ‘Name.md’
  • 2 cosmetic changes to the original format were needed
    • (1) The end of each directory label had a ‘/‘ hanging off the end in GitLab’s syntax; I wanted to remove that
    • (2) The name text that labels the file path has ‘.md’ at the end; I just wanted the name itself

Cosmetic Changes

Directory Label

  • Original output: Directory Label/
    ```
  • Directory Label/
    ```
  • Modified output: Directory Label
    ```
  • Directory Label
    ```
  • Original output: Name.md
    ```
  • Name.md
    ```
  • Mofified output: Name
    ```
  • Name
    ```

Code Changes

Edits to Directory Label

  • Original code at line 106
    1. if len(files) > 0:
    2. level = root.count(os.sep) - base_level
    3. indent = ' ' * level
    4. if root != cwd:
    5. indent = ' ' * (level - 1)
    6. md_lines.append('{0} {2} **{1}/**\n'.format(indent,
    7. os.path.basename(root),
    8. TOC_LIST_PREFIX))
  • Modified code (removed 1 symbol)
    1. if len(files) > 0:
    2. level = root.count(os.sep) - base_level
    3. indent = ' ' * level
    4. if root != cwd:
    5. indent = ' ' * (level - 1)
    6. md_lines.append('{0} {2} **{1}**\n'.format(indent, # Removed '/' after the {1}
    7. os.path.basename(root),
    8. TOC_LIST_PREFIX))
  • Original code at line 114
    1. rel_dir = '.{1}{0}'.format(os.sep, root[base_len:])
    2. for md_filename in files:
    3. indent = ' ' * level
    4. if wikilinks:
    5. md_lines.append('{0} {3} [[{2}{1}]]\n'.format(indent,
    6. os.path.splitext(md_filename)[0],
    7. rel_dir,
    8. TOC_LIST_PREFIX))
    9. else:
    10. md_lines.append('{0} {3} [{1}]({2}{1})\n'.format(indent,
    11. md_filename,
    12. rel_dir,
    13. TOC_LIST_PREFIX))
  • Modified code (added 3 lines)
    1. rel_dir = '.{1}{0}'.format(os.sep, root[base_len:])
    2. for md_filename in files:
    3. indent = ' ' * level
    4. rel_dir = rel_dir.replace('.\\','.') # Remove '.\\' from the front of the file link
    5. rel_dir = rel_dir.replace('\\','/') # Replace '\\' from the file link path with '/'
    6. md_filename = md_filename.replace('.md','') # Remove the '.md' from the file name and link
    7. if wikilinks:
    8. md_lines.append('{0} {3} [[{2}{1}]]\n'.format(indent,
    9. os.path.splitext(md_filename)[0],
    10. rel_dir,
    11. TOC_LIST_PREFIX))
    12. else:
    13. md_lines.append('{0} {3} [{1}]({2}{1})\n'.format(indent,
    14. md_filename,
    15. rel_dir,
    16. TOC_LIST_PREFIX))