Skip to content

Commit

Permalink
Release v0.1.6
Browse files Browse the repository at this point in the history
  • Loading branch information
fasilwdr committed Feb 12, 2025
1 parent ac9eea1 commit 9844347
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 27 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Generates a clean, professional table of contents
- Applies modern, responsive styling without requiring external CSS
- Simple command-line interface
- Flexible output path options

## Installation

Expand Down Expand Up @@ -48,6 +49,29 @@ It will automatically:
2. Convert it to HTML
3. Save it as `static/description/index.html`

### Custom Output Path

You can specify a custom output path using the `--output` or `-o` argument:

```bash
md2indexhtml README.md --output /path/to/output/docs.html
# or use the short form
md2indexhtml README.md -o /path/to/output/docs.html
```

This will:
1. Convert your README.md to HTML
2. Create the output directory if it doesn't exist
3. Save the converted file at the specified path

### Custom Title

You can specify a custom title for the HTML document:

```bash
md2indexhtml README.md --title "My Documentation"
```

### Python API

You can also use the package programmatically in your Python code:
Expand All @@ -58,6 +82,12 @@ from md2indexhtml import convert_md_to_html
# Convert specific file
convert_md_to_html("README.md")

# Convert with custom output path
convert_md_to_html("README.md", output_path="docs/output.html")

# Convert with custom title
convert_md_to_html("README.md", title="My Documentation")

# Or let it find a markdown file automatically
convert_md_to_html()
```
Expand Down
58 changes: 34 additions & 24 deletions md2indexhtml/converter.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
import os
import sys
import argparse
import markdown
from markdown.extensions.toc import TocExtension
from .utils import extract_title_and_headers, get_default_styles, apply_styles_to_html

__version__ = "0.1.5"
__version__ = "0.1.6"


def convert_md_to_html(md_file_path=None, title="Documentation"):
def convert_md_to_html(md_file_path=None, title="Documentation", output_path=None):
"""
Convert a Markdown file to an HTML file with inline styles.
:param md_file_path: Path to the Markdown file (optional).
If not provided, uses 'static/description/index.html'
If not provided, uses the first .md file in current directory
:param title: Title for the HTML document (optional).
:param output_path: Custom output path for the HTML file (optional).
If not provided, uses 'static/description/index.html'
"""
try:
# If md_file_path is provided as an argument
if md_file_path:
# Convert to absolute path
md_file_path = os.path.abspath(md_file_path)
# Create output path in static/description/
filename = os.path.basename(md_file_path)
base_name = os.path.splitext(filename)[0]
output_dir = os.path.join(os.path.dirname(md_file_path), 'static', 'description')
output_path = os.path.join(output_dir, 'index.html')
else:
# Use default path
output_dir = os.path.join(os.getcwd(), 'static', 'description')
output_path = os.path.join(output_dir, 'index.html')
# Look for any .md file in current directory
md_files = [f for f in os.listdir(os.getcwd()) if f.endswith('.md')]
if md_files:
Expand All @@ -40,6 +35,16 @@ def convert_md_to_html(md_file_path=None, title="Documentation"):
if not os.path.exists(md_file_path):
raise FileNotFoundError(f"Markdown file not found: {md_file_path}")

# Determine output path
if output_path:
# Use the provided output path
output_path = os.path.abspath(output_path)
output_dir = os.path.dirname(output_path)
else:
# Use default path in static/description/
output_dir = os.path.join(os.path.dirname(md_file_path), 'static', 'description')
output_path = os.path.join(output_dir, 'index.html')

# Create output directory if it doesn't exist
os.makedirs(output_dir, exist_ok=True)

Expand Down Expand Up @@ -71,8 +76,7 @@ def convert_md_to_html(md_file_path=None, title="Documentation"):
styles = get_default_styles()

# Create the HTML output with inline styles
html_output = f"""
<!DOCTYPE html>
html_output = f"""<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
Expand All @@ -91,8 +95,7 @@ def convert_md_to_html(md_file_path=None, title="Documentation"):
</div>
</div>
</body>
</html>
"""
</html>"""

# Apply styles to the HTML content
html_output = apply_styles_to_html(html_output, styles)
Expand All @@ -110,15 +113,22 @@ def convert_md_to_html(md_file_path=None, title="Documentation"):


def main():
# Get command line arguments
args = sys.argv[1:]

if args:
# If argument provided, use it as markdown file path
convert_md_to_html(args[0])
else:
# If no argument, try to convert markdown file in current directory
convert_md_to_html()
parser = argparse.ArgumentParser(
description='Convert Markdown files to styled HTML for Odoo modules'
)
parser.add_argument('file', nargs='?', help='Path to the markdown file (optional)')
parser.add_argument('--version', action='version',
version=f'md2indexhtml {__version__}')
parser.add_argument('--title', help='Specify a custom title for the HTML document')
parser.add_argument('--output', '-o', help='Specify a custom output path for the HTML file')

args = parser.parse_args()

try:
convert_md_to_html(args.file, args.title, args.output)
except Exception as e:
print(f"Error: {str(e)}", file=sys.stderr)
sys.exit(1)


if __name__ == '__main__':
Expand Down
8 changes: 6 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ build-backend = "setuptools.build_meta"

[project]
name = "md2indexhtml"
dynamic = ["version", "dependencies"]
version = "0.1.6"
description = "Convert Markdown files to index.html for Odoo modules"
readme = "README.md"
authors = [
{name = "Fasil", email = "[email protected]"}
]
requires-python = ">=3.6"
license = {text = "MIT"}
dependencies = ["markdown>=3.0"] # No longer dynamic
classifiers = [
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
Expand All @@ -32,4 +33,7 @@ keywords = ["markdown", "html", "odoo", "documentation"]
[project.urls]
Homepage = "https://github.com/fasilwdr/md2indexhtml"
"Bug Reports" = "https://github.com/fasilwdr/md2indexhtml/issues"
Source = "https://github.com/fasilwdr/md2indexhtml"
Source = "https://github.com/fasilwdr/md2indexhtml"

[project.scripts]
md2indexhtml = "md2indexhtml.converter:main"
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='md2indexhtml',
version='0.1.5',
version='0.1.6',
description='Convert Markdown files to index.html for Odoo modules',
long_description=long_description,
long_description_content_type='text/markdown',
Expand Down

0 comments on commit 9844347

Please sign in to comment.