Can somebody help me to convert markdown to html using python language.
I'm not a programmer but i want to understand some basic things in programming, and now i'm trying to make this conversion.

Can somebody show me for example how to convert this:
https://gist.githubusercontent.com/Irene26/6da8be01b46f07bf2a48/raw/6ef8e49a7c50e0aa1ae7465f6069f0a262b0ad84/gistfile1.txt

to look like this:

https://gist.githubusercontent.com/Irene26/a05c52826fcd680cbd74/raw/22a44879753b137e28d25affefa21912dfdb6912/gistfile1.txt

I'll be very thankfull if somebody will help me to understand this thing.

I was trying to do something like this. But i think it's not a good idea.

import os
import sys
import fileinput


searchquery = '#'
searchquery2 = '##'
searchquery3 = '######'
prefix = '<h1>'
suffix = '</h1>'
prefix2 = '<h2>'
suffix2 = '</h2>'
prefix3 = '<h6>'
suffix3 = '</h6>'
paragraph1 = '<p>'
paragraph2 = '</p>'

with open('file.txt') as f1:
    with open('file2.txt', 'a') as f2:
        lines = f1.readlines()
        for i, line in enumerate(lines):
            if i == 0:

                if line.startswith(searchquery3):
                    line.replace("######", "")
                    f2.write('%s%s%s' % (prefix3, line[7:].rstrip('\n'), suffix3))
                elif line.startswith(searchquery2):
                    line.replace("##", "")
                    f2.write('%s%s%s' % (prefix2, line[3:].rstrip('\n'), suffix2))
                elif line.startswith(searchquery):
                    line.replace("#", "")
                    f2.write('%s%s%s' % (prefix, line[2:].rstrip('\n'), suffix))

            if i > 0:

                if line.startswith(searchquery3):
                    line.replace("######", "")
                    f2.write('\n\n%s%s%s' % (prefix3, line[7:].rstrip('\n'), suffix3))
                elif line.startswith(searchquery2):
                    line.replace("##", "")
                    f2.write('\n\n%s%s%s' % (prefix2, line[3:].rstrip('\n'), suffix2))
                elif line.startswith(searchquery):
                    line.replace("#", "")
                    f2.write('\n\n%s%s%s\n' % (prefix, line[2:].rstrip('\n'), suffix))

            if not line.startswith('#') and line not in ['\n', '\r\n']:
                f2.write('\n\n%s%s%s' % (paragraph1, line.rstrip('\n'), paragraph2))

Hm, you want to understand some basic things in programming, but the task of converting markdown to html is not such a basic thing. I would recommend it only to experienced programmers.

Such modules already exist: you can install the markdown module, then use its conversion function described here

import codecs
import markdown
input_file = codecs.open("some_file.txt", mode="r", encoding="utf-8")
text = input_file.read()
html = markdown.markdown(text)

You could perhaps look in the source code of the markdown module to see how hard it is.

Edited 1 Year Ago by Gribouillis

Comments
can you send me link with source code of the markdown module, i'll be very thankful

There is also a markdown2 module.

import markdown2

markdown = """\
# Hello, how are you
I'm fine, thank you

I know that it is not so **hard**
but for me it's not so *easy*

What is ##this?

How about `*this*`?

**it *wor`k`s!***

Let me put a bold link: [**stuff**](http://41.media.tumblr.com/49a58542fd70b8ca39b5bd0d9c9c53aa/tumblr_nob40mvTN41tb9nzio1_500.jpg)
## Okay, part two

But there's no part two!

###### So long!"""

print(markdown2.markdown(markdown))

"""Output-->
<h1>Hello, how are you</h1>

<p>I'm fine, thank you</p>

<p>I know that it is not so <strong>hard</strong>
but for me it's not so <em>easy</em></p>

<p>What is ##this?</p>

<p>How about <code>*this*</code>?</p>

<p><strong>it <em>wor<code>k</code>s!</em></strong></p>

<p>Let me put a bold link: <a href="http://41.media.tumblr.com/49a58542fd70b8ca39b5bd0d9c9c53aa/tumblr_nob40mvTN41tb9nzio1_500.jpg"><strong>stuff</strong></a></p>

<h2>Okay, part two</h2>

<p>But there's no part two!</p>

<h6>So long!</h6>
"""

Edited 1 Year Ago by snippsat

In order to see the source code, install the markdown or markdown2 module first, then visit the folder named markdown or markdown2. You can type in python

>>> import markdown
>>> print(markdown)

This should give you the path to the folder. All the python files in this folder are part of the module's source code.

Edited 1 Year Ago by Gribouillis

This article has been dead for over six months. Start a new discussion instead.