Skip to content
Advertisement

Autogenerate Markdown index from a recursive folder with makdown files in python

Hi I have a folder with many subfolders with markdown (.md) files and I want to generate an index for this. I try this

MD_FILE_LIST = (y for x in os.walk(DOC_FILES_DIR)
                for y in glob(os.path.join(x[0], '*.md')))

with open(DOC_FILES_DIR + 'index.md', 'w') as f:
    text = []
    for m_file in MD_FILE_LIST:
        text.append('%s[%s](/%s)n' % (m_file.count(os.sep)*' ',
                                  m_file.split(os.sep)[-1].replace('.md', ''),
                                  m_file.replace(DOC_FILES_DIR, '').replace(os.sep,'/')))

    f.writelines(text)

But the generated file did´t show a linked html view, it’s shown as a plain text [name](url). as I shown in the picture And also if it’s possible create a heading at least for the 2 or 3 first levels

enter image description here

NEW version

I made some fix so now looks like THIS here is my the updated version code:

with open(DOC_FILES_DIR + 'README.md', 'w') as f:
    text = []
    for m_file in MD_FILE_LIST:
        levels = m_file.replace(DOC_FILES_DIR, '').count(os.sep)
        if levels < 3:
            text.append( ' * ' + '#' * levels + ' [%s](./%s)n' % (
                                    m_file.split(os.sep)[-1].replace('.md', ''),
                                    m_file.replace(DOC_FILES_DIR, '').replace(os.sep,'/')))
        else:
            text.append('%s[%s](./%s)n' % (m_file.count(os.sep)*' ' + '- ',
                        m_file.split(os.sep)[-1].replace('.md', ''),
                        m_file.replace(DOC_FILES_DIR, '').replace(os.sep, '/')))
    text[0] = "# Indexn"
    f.writelines(text)

but seems that I can’t use headings and list at the same time

Advertisement

Answer

You need a blank line between each list item.

A strict reading of the Markdown rules suggests that a block level element cannot be included in a list item unless it is a “loose” list, that is, it is surrounded by blank lines. For example, consider this simple list:

* Item 1
* Item 2

It gets rendered as:

<ul>
    <li>Item 1</li>
    <li>Item 2</li>
</ul>

Notice that the content of the list item gets inserted as inline text. No block level constructs.

However, this list:

* Item 1

* Item 2

Gets rendered as:

<ul>
    <li>
        <p>Item 1</p>
    </li>
    <li>
        <p>Item 2</p>
    </li>
</ul>

Notice that the content of each list item was placed inside a block level paragraph (<p>). And the only difference was that a blank line was added between the items.

This same technique can be used for other block level constructs. For example, to make a heading in Markdown, you precede the text by a number of hashes and at least one space:

* # Item 1

* # Item 2

Which results in:

<ul>
    <li>
        <h1>Item 1</h1>
    </li>
    <li>
        <h1>Item 2</h1>
    </li>
</ul>

Now let’s try that with the first few lines of your document:

# Index

* ## [combustionModel](./combustionModels/combustionModel/combustionModel.md)

* ## [diffusion](./combustionModels/diffusion/diffusion.md)

* ## [FSD.T](./combustionModels/FSD/FSD.T.md)

    - [consumptionSpeed](./combustionModels/FSD/reactionRateFlameAreaModels/consumptionSpeed/consumptionSpeed.md)
    - [reactionRateFlameArea](./combustionModels/FSD/reactionRateFlameAreaModels/reactionRateFlameArea/reactionRateFlameArea.md)
    - [relaxation](./combustionModels/FSD/reactionRateFlameAreaModels/relaxation/relaxation.md)

* ## [infinitelyFastChemistry](./combustionModels/infinitelyFastChemistry/infinitelyFastChemistry.md)

Which gets rendered as:

<h1>Index</h1>

<ul>
    <li>
        <h2><a href="./combustionModels/combustionModel/combustionModel.md">combustionModel</a></h2>
    </li>
    <li>
        <h2><a href="./combustionModels/diffusion/diffusion.md">diffusion</a></h2>
    </li>
    <li>
        <h2><a href="./combustionModels/FSD/FSD.T.md">FSD.T</a></h2>

        <ul>
            <li><a href="./combustionModels/FSD/reactionRateFlameAreaModels/consumptionSpeed/consumptionSpeed.md">consumptionSpeed</a>
            </li>
            <li><a href="./combustionModels/FSD/reactionRateFlameAreaModels/reactionRateFlameArea/reactionRateFlameArea.md">reactionRateFlameArea</a>
            </li>
            <li><a href="./combustionModels/FSD/reactionRateFlameAreaModels/relaxation/relaxation.md">relaxation</a>
            </li>
        </ul>
    </li>
    <li>
        <h2><a href="./combustionModels/infinitelyFastChemistry/infinitelyFastChemistry.md">infinitelyFastChemistry</a></h2>
    </li>
</ul>

Notice that I left the nested list (which doesn’t have headers) without blank lines, so those list items do not get wrapped in paragraphs. If you would prefer paragraphs, then just include blank lines there as well.

The edit to you code is pretty simple. Just add an extra n to line six:

            text.append( ' * ' + '#' * levels + ' [%s](./%s)nn' % (

as well as line 14 (while not technically required, it is good form to always have a blank line after a header in Markdown):

    text[0] = "# Indexnn"

And if you want the nested sublists to be block level, also line 10:

            text.append('%s[%s](./%s)nn' % (m_file.count(os.sep)*' ' + '- ',
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement