Markdown to HTML

Hello,

I’m trying to write a Python script that will convert markdown files to HTML files. Here are some of the requirements and test cases:

Write a script markdown2html.py that takes an argument 2 strings:

  • First argument is the name of the Markdown file
  • Second argument is the output file name

Requirements:

  • If the number of arguments is less than 2: print in STDERR Usage: ./markdown2html.py README.md README.html and exit 1
  • If the Markdown file doesn’t exist: print in STDER Missing <filename> and exit 1
  • Otherwise, print nothing and exit 0

Test case 1

nzubeifechukwu@Nzube:~/$ cat README.md
# My title
## My title2
# My title3
#### My title4
### My title5

nzubeifechukwu@Nzube:~/$ ./markdown2html.py README.md README.html 
nzubeifechukwu@Nzube:~/$ cat README.html 
<h1>My title</h1>
<h2>My title2</h2>
<h1>My title3</h1>
<h4>My title4</h4>
<h3>My title5</h3>
nzubeifechukwu@Nzube:~/$ 

Test case 2

nzubeifechukwu@Nzube:~/$ cat README.md
# My title
- Hello
- Bye

nzubeifechukwu@Nzube:~/$ ./markdown2html.py README.md README.html 
nzubeifechukwu@Nzube:~/$ cat README.html 
<h1>My title</h1>
<ul>
<li>Hello</li>
<li>Bye</li>
</ul>
nzubeifechukwu@Nzube:~/$ 

This is what I have done so far for these two cases:

#!/usr/bin/python3
'''This script takes two arguments: first is a markdown file name and the
    second is an html output filename
'''


if __name__ == '__main__':
    import sys
    import os
    if len(sys.argv) < 3:
        print('Usage: ./markdown2html.py README.md README.html',
              file=sys.stderr)
        sys.exit(1)

    if not os.path.isfile(sys.argv[1]):
        print('Missing {}'.format(sys.argv[1]), file=sys.stderr)
        sys.exit(1)

    sym_map = {
            '# ': ('<h1>', '</h1>\n'),
            '## ': ('<h2>', '</h2>\n'),
            '### ': ('<h3>', '</h3>\n'),
            '#### ': ('<h4>', '</h4>\n'),
            '##### ': ('<h5>', '</h5>\n'),
            '###### ': ('<h6>', '</h6>\n')
        }
    ul = []

    with open(sys.argv[1]) as rf:
        read_lines = rf.readlines()
        with open(sys.argv[2], 'a') as wf:
            for line in read_lines:
                if line.startswith('- '):
                    ul.append(line)

                if ul:
                    i = 0
                    while i < len(ul):
                        if i == 0:
                            html_line = ul[i].replace('- ', '<ul>\n<li>').replace(ul[i][-1], '</li>\n')
                        else:
                            html_line = ul[i].replace('- ', '<li>').replace('\n', '</li>\n')
                        if i == len(ul) - 1:
                            html_line = html_line.replace(html_line[-1], '\n</ul>\n')
                            wf.write(html_line)
                        else:
                            wf.write(html_line)
                        i = i + 1

                for k, v in sym_map.items():
                    if line.startswith(k):
                        html_line = line.replace(k, v[0]).replace('\n', v[1])
                        break
                wf.write(html_line)

    sys.exit(0)

This is my output. My code gives the correct output for test case 1, but not for test case (the issue is with the unordered lists). Here is the output:

<h1>My title</h1>
<ul></li>
</ul>
<li>Hello</li>
</ul>
<ul></li>
</ul>
<li>Hello</li>
</ul>
<ul></li>
<li>Hello</li>
<li>Bye</li>
</ul>
<li>Bye</li>
</ul>

Please how do I get this to render the correct output?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.