Skip to content
This repository was archived by the owner on Dec 14, 2024. It is now read-only.

Commit 38696c3

Browse files
Enhance CLI and Documentation for Markdown Server and Converter
1 parent 1ebc5c3 commit 38696c3

File tree

8 files changed

+283
-128
lines changed

8 files changed

+283
-128
lines changed

README.rst

Lines changed: 78 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
.. image:: https://raw.githubusercontent.com/PtPrashantTripathi/markdown-server/master/markdownserver-logo.webp
2+
:alt: "Markdown Server Logo"
3+
:width: 300px
4+
:align: center
5+
16
===============
27
Markdown Server
38
===============
49

510
Markdown-server is a simple web application.
6-
It converts markdown file to HTML and response by `text/html`.
11+
It converts markdown files to HTML and responds with `text/html`.
712

813
How to use
914
==========
@@ -29,63 +34,112 @@ Just try
2934
Start server
3035
------------
3136

32-
You don't need any special preparation to try to start server. Just execute below commands.
37+
You can install `markdown-server` from **PyPi** or run it directly from the repository.
3338

39+
### Install from PyPi:
40+
::
41+
42+
(.venv)$ pip install markdown-server
43+
(.venv)$ markdownserver --host localhost --port 8009 --debug
44+
45+
Bottle v0.12.8 server starting up (using WSGIRefServer())...
46+
Listening on http://localhost:8009/
47+
48+
### Install from GitHub:
3449
::
3550

3651
$ git clone https://github.com/ohbarye/markdown-server
3752
$ cd markdown-server
3853
$ virtualenv .venv
3954
$ source .venv/bin/activate
4055
(.venv)$ pip install -r requirements.txt
41-
(.venv)$ markdownserver
56+
(.venv)$ markdownserver --host localhost --port 8009 --debug
4257
Bottle v0.12.8 server starting up (using WSGIRefServer())...
4358
Listening on http://localhost:8009/
4459

45-
Or, you can install from PyPi.
60+
If the server starts up successfully, browse the below URL and check the converted result.
4661

4762
::
4863

49-
(.venv)$ pip install markdown-server
50-
(.venv)$ markdownserver
51-
Bottle v0.12.8 server starting up (using WSGIRefServer())...
52-
Listening on http://localhost:8009/
64+
$ open http://localhost:8009/sample.md
5365

66+
MarkdownServer CLI
67+
------------------
5468

55-
If server start up successfully, browse below URL and check the converted result.
69+
You can run the `markdownserver` command to start the server with the following examples:
5670

71+
### Example 1: Using Default Arguments
5772
::
5873

59-
$ open http://localhost:8009/sample.md
74+
(.venv)$ markdownserver
75+
76+
This will run the server on the default host (`localhost`) and port (`8009`), with debugging disabled.
77+
78+
### Example 2: Custom Host, Port, and Debug
79+
::
80+
81+
(.venv)$ markdownserver --host 0.0.0.0 --port 8080 --debug
82+
83+
This will run the server on `0.0.0.0:8080` with debugging enabled, allowing you to access the server from any device on your local network.
84+
85+
**Arguments:**
86+
- `--host`: Specify the host to run the server on (default is `localhost`).
87+
- `--port`: Specify the port to run the server on (default is `8009`).
88+
- `--debug`: Enable or disable debugging mode (default is `False`).
6089

6190
Only Conversion
6291
---------------
6392

64-
Additionally, You can use the conversion function alone.
93+
Additionally, you can use the conversion function alone from the command line.
94+
95+
::
96+
97+
(.venv)$ markdownconvert source_md_file target_html_file
98+
99+
This will convert the provided markdown file (`source_md_file`) into an HTML file (`target_html_file`).
100+
101+
MarkdownConverter CLI
102+
---------------------
103+
104+
The `markdownconvert` command can be used with the following options:
65105

66106
::
67107

68-
(.venv)$ convert source_md_file target_html_file
108+
$ markdownconvert source_md_file target_html_file
109+
110+
**Arguments:**
111+
- `source_md_file`: The markdown file you want to convert to HTML.
112+
- `target_html_file`: The output HTML file to save the converted content.
113+
114+
Example:
115+
116+
::
117+
118+
$ markdownconvert sample.md sample.md.html
119+
120+
This will convert the `sample.md` file into a `sample.md.html` file in the current directory.
69121

70122
--------------
71123
Do as you like
72124
--------------
73125

74-
- Markdown server purvey `http://host/[file_name]` URL. This corresponds to `resources/markdown/[file_name]`.You can put any markdown file here.
75-
76-
- Converted file will be placed to `resources/html` directory. Generated html file includes CSS so it's ease to distribute.
77-
78-
- Environment variables like *host name* or *port number* are set in `env.py`. Edit arbitrarily.
126+
- The Markdown server provides `http://host/[file_name]` URL. You can place any markdown file here.
79127

80-
::
128+
- The converted HTML file will be placed in the `resources/html` directory. The generated HTML file includes CSS for easy distribution.
81129

82-
ms_port = '8009'
83-
ms_host = 'localhost'
130+
Developers Information
131+
======================
84132

133+
1. **Masato Ohba**
134+
- GitHub: `@ohbarye <https://github.com/ohbarye>`
135+
.. image:: https://avatars.githubusercontent.com/u/1811616
136+
:alt: "Masato Ohba"
137+
:width: 200px
85138

86-
- The default markdown engine is Github flavored Markdown. If you want to use another style, add CSS and edit `env.py`.
139+
2. **Pt. Prashant Tripathi**
140+
- GitHub: `@ptprashanttripathi <https://github.com/ptprashanttripathi>`
141+
.. image:: https://avatars.githubusercontent.com/u/26687933
142+
:alt: "Pt. Prashant Tripathi"
143+
:width: 200px
87144

88-
::
89145

90-
css_name = 'github.css'
91-
markdown_type = 'gfm'

markdownserver-logo.webp

206 KB
Loading

markdownserver/__init__.py

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,8 @@
1-
from __future__ import absolute_import
2-
import argparse
3-
from bottle import route, run, static_file
41
from .markdown_converter import MarkdownConverter
5-
from .env import root_path, ms_host, ms_port, ms_debug
6-
import os
2+
from .markdown_server import MarkdownServer
73

8-
9-
parser = argparse.ArgumentParser()
10-
parser.add_argument('--host')
11-
parser.add_argument('--port')
12-
parser.add_argument('--debug', action='store_true')
13-
args = parser.parse_args()
14-
15-
converter = MarkdownConverter()
16-
17-
18-
@route(r'/<resource:re:.*\.md>')
19-
def gfmize(resource):
20-
if resource == 'favicon.ico':
21-
return ''
22-
23-
html_file_name = os.path.basename(converter.convert(resource))
24-
if '/' in resource:
25-
html_file_name = '/'.join(resource.split('/')[:-1]) + \
26-
'/' + html_file_name
27-
return static_file(os.path.join('resources/html',
28-
html_file_name),
29-
root=root_path)
30-
31-
32-
def main():
33-
run(host=args.host or ms_host,
34-
port=args.port or ms_port,
35-
debug=args.debug or ms_debug,
36-
reloader=False)
37-
38-
39-
if __name__ == '__main__':
40-
main()
4+
__author__ = [
5+
{"name": "Masato Ohba", "email": "over.rye@gmail.com"},
6+
{"name": "Pt. Prashant Tripathi", "email": "ptprashanttripathi@outlook.com"},
7+
]
8+
__all__ = ["MarkdownServer", "MarkdownConverter"]

markdownserver/markdown_converter.py

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
from __future__ import print_function
2-
from __future__ import absolute_import
3-
from builtins import object
4-
import markdown as md
5-
import codecs
61
import sys
72
import os
8-
from .env import css_path, ms_encoding, markdown_type, \
9-
html_dir, html_extension
3+
import markdown as md
4+
import codecs
5+
import argparse
6+
from .env import css_path, ms_encoding, markdown_type, html_dir, html_extension
107

118

12-
class MarkdownConverter(object):
9+
class MarkdownConverter:
1310
def __init__(self):
11+
"""Initialize the converter with the CSS header and footer."""
1412
css = codecs.open(css_path, encoding=ms_encoding, mode="r")
1513
self.html_header = (
1614
"""
@@ -35,21 +33,21 @@ def __init__(self):
3533
"""
3634

3735
def convert(self, src, dst=""):
36+
"""Convert a Markdown file to HTML."""
3837
code = md.markdown(self.read_md(src), extensions=[markdown_type])
3938
return self.write_html(code, src, dst)
4039

4140
def read_md(self, file_name):
41+
"""Read the contents of a Markdown file."""
4242
workingdir = os.getcwd()
4343
md_file = codecs.open(
44-
os.path.join(workingdir, file_name),
45-
encoding=ms_encoding,
46-
mode="r"
44+
os.path.join(workingdir, file_name), encoding=ms_encoding, mode="r"
4745
)
4846
return md_file.read()
4947

5048
def write_html(self, body, file_name, dst):
49+
"""Write HTML content to a file."""
5150
html_path = os.path.join(html_dir, file_name + html_extension)
52-
5351
if dst != "":
5452
html_path = dst
5553
try:
@@ -63,12 +61,29 @@ def write_html(self, body, file_name, dst):
6361

6462

6563
def main():
66-
args = sys.argv
67-
if len(args) != 3:
68-
print("usage: convert source_md_file target_html_file")
69-
else:
70-
converter = MarkdownConverter()
71-
converter.convert(args[1], args[2])
64+
"""CLI entry point to convert a Markdown file to HTML using argparse."""
65+
parser = argparse.ArgumentParser(
66+
description="Convert a Markdown file to an HTML file with styling."
67+
)
68+
parser.add_argument(
69+
"source_md_file",
70+
metavar="SOURCE_MD_FILE",
71+
help="The source Markdown file to convert",
72+
)
73+
parser.add_argument(
74+
"target_html_file",
75+
metavar="TARGET_HTML_FILE",
76+
help="The destination HTML file to save",
77+
)
78+
79+
args = parser.parse_args()
80+
81+
# Convert the given Markdown file to HTML
82+
converter = MarkdownConverter()
83+
html_path = converter.convert(args.source_md_file, args.target_html_file)
84+
print(
85+
f"Markdown file '{args.source_md_file}' converted to HTML and saved as '{html_path}'"
86+
)
7287

7388

7489
if __name__ == "__main__":

0 commit comments

Comments
 (0)