Have the same README both in Markdown and reStructuredText

后端 未结 8 1261
旧时难觅i
旧时难觅i 2020-12-22 16:29

I have a project hosted on GitHub. For this I have written my README using the Markdown syntax in order to have it nicely formatted on GitHub.

As my project is in Py

相关标签:
8条回答
  • 2020-12-22 17:01

    I ran into this problem and solved it with the two following bash scripts.

    Note that I have LaTeX bundled into my Markdown.

    #!/usr/bin/env bash
    
    if [ $# -lt 1 ]; then
      echo "$0 file.md"
      exit;
    fi
    
    filename=$(basename "$1")
    extension="${filename##*.}"
    filename="${filename%.*}"
    
    if [ "$extension" = "md" ]; then
      rst=".rst"
      pandoc $1 -o $filename$rst
    fi
    

    Its also useful to convert to html. md2html:

    #!/usr/bin/env bash
    
    if [ $# -lt 1 ]; then
      echo "$0 file.md <style.css>"
      exit;
    fi
    
    filename=$(basename "$1")
    extension="${filename##*.}"
    filename="${filename%.*}"
    
    if [ "$extension" = "md" ]; then
      html=".html"
      if [ -z $2 ]; then
        # if no css
        pandoc -s -S --mathjax --highlight-style pygments $1 -o $filename$html
      else
        pandoc -s -S --mathjax --highlight-style pygments -c $2 $1 -o $filename$html
      fi
    fi
    

    I hope that helps

    0 讨论(0)
  • 2020-12-22 17:03

    As @Chris suggested, you can use Pandoc to convert Markdown to RST. This can be simply automated using pypandoc module and some magic in setup.py:

    from setuptools import setup
    try:
        from pypandoc import convert
        read_md = lambda f: convert(f, 'rst')
    except ImportError:
        print("warning: pypandoc module not found, could not convert Markdown to RST")
        read_md = lambda f: open(f, 'r').read()
    
    setup(
        # name, version, ...
        long_description=read_md('README.md'),
        install_requires=[]
    )
    

    This will automatically convert README.md to RST for the long description using on PyPi. When pypandoc is not available, then it just reads README.md without the conversion – to not force others to install pypandoc when they wanna just build the module, not upload to PyPi.

    So you can write in Markdown as usual and don’t care about RST mess anymore. ;)

    0 讨论(0)
提交回复
热议问题