📜 ⬆️ ⬇️

Web application for working with markdown notes


For a long time I have accumulated a lot of different notes, tips and cheat sheets on various topics, both related to it and completely unrelated. There is a need to store it conveniently and structured.

I tried different wiki engines, but not everything I liked about them, sometimes the functionality was not enough, and sometimes it was too much. I thought that if you want to do well - do it yourself you need to write your bike.

I have long wanted to write a web application in Python, so the choice fell on this language. Under the cut source code and description, as well as a link to the repository.

The essence of this application is as follows: there is a directory with md files, it is convenient to write them and it is convenient to use in addition to this application, if the file is called test.md, then the localhost: 8000 / test address will open the contents of this file in the html display.
')
First, create a display template for the ginger:

<!DOCTYPE html> <html lang="ru"> <head> <meta charset="UTF-8"> <title>{{ title }}</title> <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='normalize.css') }}"> <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='codehilite.css') }}"> <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}"> </head> <body> <table> <tr> <td valign="top" class="side"> <h1 class="logo"> <a href="/">{{ title }}</a> </h1> {{ side|safe }} </td> <td valign="top"> {{ content|safe }} </td> </tr> </table> </body> </html> 

It is very simple and uses a table to separate the side menu from the main content.

Now you need to make a configuration file:

 #   [app_setting] #   start_page=start #    side_bar=sidebar #   md  pages_folder=pages #     port=8000 #     not_found_text=    #   main_title= 

And parse it:

 class ConfigProvider: config_file_name = 'config.ini' start_page = 'start' side_bar = 'sidebar' pages_folder = 'pages' port = '8080' not_found_text = 'Page Not Found' main_title = u' ' def __init__(self): #      if function.check_found_file(self.config_file_name): config = ConfigParser.RawConfigParser() config.read(self.config_file_name) #     self.start_page = config.get('app_setting', 'start_page').decode('utf8') self.side_bar = config.get('app_setting', 'side_bar').decode('utf8') self.pages_folder = config.get('app_setting', 'pages_folder').decode('utf8') self.port = config.getint('app_setting', 'port') self.not_found_text = config.get('app_setting', 'not_found_text').decode('utf8') self.main_title = config.get('app_setting', 'main_title').decode('utf8') 


The check_found_file () used in the last block of the code indicates whether the requested file exists:

 def check_found_file(filename): """    """ try: file = open(filename) except IOError as e: return False else: with file: return True 


The most important function in this application is the markdown to html conversion:

 def markdown_to_html(filename): """  markdown   html  """ f = open(config.pages_folder + '/' + filename + '.md', 'r') all_file = '' for st in f.readlines(): all_file += st html = markdown.markdown(all_file.decode('utf8'), extensions=['codehilite']) return html 


Now it remains to get the contents of the page:

 #    def get_page(name): #    if function.check_found_file(config.pages_folder + '/' + name + '.md'): page = function.markdown_to_html(name) else: page = config.not_found_text return page 


And show in browser
 @app.route('/') def hello(): return render_template( 'page.html', title=config.main_title, side=get_page(config.side_bar), content=get_page(config.start_page) ) @app.route('/<name>') def page(name): return render_template( 'page.html', title=config.main_title, side=get_page(config.side_bar), content=get_page(name) ) if __name__ == '__main__': app.run(port=config.port) 


That's all. Notes are created and edited in your favorite editor, and are read in the browser.

To synchronize all this between their devices, you can use the cloud service.

I hope this application will help you put your notes and notes "on the shelves" and, maybe, get to know and make friends with such an excellent language like python.

useful links


Project repository on GitHub
Markdown syntax (official documentation)
Markdown syntax (article in Russian)
Flask official website

Source: https://habr.com/ru/post/307318/


All Articles