Skip to content

Commit 6bd44f3

Browse files
Update README with usage documentation for convert_markdown function and refactor related code
1 parent 92f31b5 commit 6bd44f3

5 files changed

Lines changed: 76 additions & 14 deletions

File tree

README.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,73 @@ pip install md2all
3333
- Support for PDF generation
3434
- Support for custom themes
3535
- Support for custom fonts
36+
37+
38+
## How to Use it
39+
40+
Sure! Here's a concise and developer-friendly **documentation** for using the `convert_markdown` function as part of your Python library (assuming it's installed from PyPI):
41+
42+
---
43+
44+
## 📄 `convert_markdown`
45+
46+
Convert a Markdown (`.md`) file into a styled HTML file with TailwindCSS and MathJax support.
47+
48+
---
49+
50+
### 🔧 Function Signature
51+
52+
```python
53+
convert_markdown(md_path: str, output_dir: str = "", output_format: str = "pdf") -> str
54+
```
55+
56+
---
57+
58+
### ✨ Features
59+
60+
- Converts `.md` files to clean, styled **HTML**
61+
- Supports **LaTeX math** using `$$...$$` (rendered via MathJax)
62+
- Automatically applies Tailwind CSS and custom styling
63+
- Resolves relative paths and saves output to the desired directory
64+
65+
---
66+
67+
### 📥 Parameters
68+
69+
| Name | Type | Description |
70+
|-----------------|--------|-----------------------------------------------------------------------------|
71+
| `md_path` | str | Path to the input Markdown file. Can be relative or absolute. |
72+
| `output_dir` | str | *(Optional)* Directory to save the converted file. Defaults to input file's directory. |
73+
| `output_format` | str | *(Optional)* Set to `"html"` to output HTML. Default is `"pdf"`, but PDF is not implemented yet. |
74+
75+
---
76+
77+
### 📤 Returns
78+
79+
- Full path to the converted file.
80+
81+
---
82+
83+
### 🧪 Example Usage
84+
85+
```python
86+
from md2all import convert_markdown
87+
88+
# Convert a Markdown file to HTML
89+
output_file = convert_markdown("notes/my_math_notes.md", output_dir="output", output_format="html")
90+
91+
print("File saved to:", output_file)
92+
```
93+
94+
---
95+
96+
### 📁 Output Structure
97+
98+
- create a `.html` file to your specified `output_dir`, if not provided, it will be saved in the same directory as the input file.
99+
- Includes all CSS and MathJax setup in the HTML head section. all css and js library are inside /home/.lib/
100+
101+
---
102+
103+
## Author
104+
105+
- Deepak Raj

md2all/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from md2all.md_to_html import transform
1+
from md2all.md_to_html import convert_markdown
22

33
__all__ = [
4-
"transform",
4+
"convert_markdown",
55
]

md2all/md_to_html.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ def setup_tailwind():
4848
dst_path = copy_file(source_tailwind_path)
4949
return dst_path
5050

51-
from bs4 import BeautifulSoup
52-
5351
def modify_classes(html_content):
5452
"""Modify HTML content by injecting Tailwind classes into elements using BeautifulSoup."""
5553
soup = BeautifulSoup(html_content, 'html.parser')
@@ -86,12 +84,7 @@ def read_markdown_file(file_path):
8684
with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
8785
return f.read()
8886

89-
import os
90-
import shutil
91-
from pathlib import Path
92-
import markdown
93-
94-
def transform(md_path, output_dir="", output_format="pdf"):
87+
def convert_markdown(md_path, output_dir="", output_format="pdf"):
9588
"""Convert Markdown to HTML (optionally PDF), save results in appropriate location."""
9689
# get full path if not absolute
9790
if not os.path.isabs(md_path):
@@ -138,15 +131,15 @@ def transform(md_path, output_dir="", output_format="pdf"):
138131
f.write(html_template)
139132

140133
# If output format is .html, copy to output directory
141-
if output_format == ".html":
134+
if output_format == "html":
142135
if output_dir:
143136
setup_directory(output_dir)
144137
shutil.copy(temp_html_path, final_output_path)
145138
else:
146139
shutil.copy(temp_html_path, final_output_path)
147140

148141
# If output format is .pdf, delegate to PDF converter
149-
elif output_format == ".pdf":
142+
elif output_format == "pdf":
150143
if output_dir:
151144
setup_directory(output_dir)
152145
# Here, you'd use `temp_html_path` for PDF conversion

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# Setting up the package
1313
setuptools.setup(
1414
name="md2all",
15-
version="0.0.2",
15+
version="0.0.3",
1616
author="Deepak Raj",
1717
author_email="deepak008@live.com",
1818
description="convertor is a simple and easy to use library for converting markdown files to various formats.",

test.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)