66from bs4 import BeautifulSoup
77
88ROOT_DIR = Path (__file__ ).resolve ().parent
9- print ("Root Directory:" , ROOT_DIR )
10- # Get full path to user's home and centralized .lib directory
119home_dir = os .path .expanduser ("~" )
1210lib_dir = os .path .join (home_dir , ".lib" )
1311
@@ -88,43 +86,44 @@ def read_markdown_file(file_path):
8886 with open (file_path , "r" , encoding = "utf-8" , errors = "ignore" ) as f :
8987 return f .read ()
9088
91- def transform (md_path , output_dir = "" , output_format = "" ):
92- """Convert Markdown text to HTML with Tailwind CSS classes and MathJax setup."""
93- print ("Processing Markdown file:" , md_path )
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" ):
95+ """Convert Markdown to HTML (optionally PDF), save results in appropriate location."""
96+ # get full path if not absolute
97+ if not os .path .isabs (md_path ):
98+ md_path = os .path .abspath (md_path )
9499 markdown_text = read_markdown_file (md_path )
95100 markdown_text = convert_latex_format (markdown_text )
96101
97- if output_dir :
98- setup_directory (output_dir )
99- output_dir = os .path .join (output_dir , os .path .basename (md_path ).replace (".md" , f".{ output_format } " ))
100- else :
101- output_dir = md_path .replace (".md" , f".{ output_format } " )
102+ # Prepare file names and paths
103+ base_name = os .path .basename (md_path ).replace (".md" , "" )
104+ temp_html_path = os .path .join ("/tmp" , f"{ base_name } .html" )
105+ final_output_path = os .path .join (output_dir , f"{ base_name } .{ output_format } " ) if output_dir else md_path .replace (".md" , f".{ output_format } " )
102106
103- html_content = markdown .markdown (markdown_text , extensions = ['md_in_html' , 'fenced_code' , 'codehilite' , 'toc' , 'attr_list' ])
107+ # Convert to HTML content
108+ html_content = markdown .markdown (
109+ markdown_text ,
110+ extensions = ['md_in_html' , 'fenced_code' , 'codehilite' , 'toc' , 'attr_list' ]
111+ )
104112 html_content = modify_classes (html_content )
105-
106- html_template = """
113+
114+ # Inject into template
115+ html_template = f"""
107116 <!DOCTYPE html>
108117 <html lang="en" class="scroll-smooth bg-gray-50 text-gray-900 antialiased">
109118 <head>
110119 <meta charset="UTF-8">
111120 <meta name="viewport" content="width=device-width, initial-scale=1.0">
112-
113- <title>{title}</title>
114-
115- <!-- Tailwind CSS -->
116- <link href="{centralized_tailwind_path}" rel="stylesheet">
117-
118- <!-- MathJax -->
119- <script type="text/javascript" id="MathJax-script" async
120- src="{centralized_mathjax_path}"></script>
121-
122- <!-- Custom CSS -->
123- <link rel="stylesheet" href="{centralized_css_path}" />
121+ <title>{ base_name } </title>
122+ <link href="{ Path (setup_tailwind ()).as_uri ()} " rel="stylesheet">
123+ <script type="text/javascript" id="MathJax-script" async src="{ Path (setup_mathjax ()).as_uri ()} "></script>
124+ <link rel="stylesheet" href="{ Path (setup_custom_css ()).as_uri ()} " />
124125 </head>
125126 <body for="html-export" class="min-h-screen flex flex-col justify-between">
126-
127- <!-- Main content -->
128127 <main class="flex-1">
129128 <div class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 py-8 prose prose-lg prose-slate">
130129 { html_content }
@@ -133,13 +132,25 @@ def transform(md_path, output_dir="", output_format=""):
133132 </body>
134133 </html>
135134 """
136-
137- html_template = html_template .replace ("{title}" , "Markdown to HTML Conversion" )
138- html_template = html_template .replace ("{html_content}" , html_content )
139- html_template = html_template .replace ("{centralized_mathjax_path}" , Path (setup_mathjax ()).as_uri ())
140- html_template = html_template .replace ("{centralized_css_path}" , Path (setup_custom_css ()).as_uri ())
141- html_template = html_template .replace ("{centralized_tailwind_path}" , Path (setup_tailwind ()).as_uri ())
142135
143-
144- with open (output_dir , "w" , encoding = "utf-8" ) as f :
136+ # Save temporary HTML to /tmp
137+ with open (temp_html_path , "w" , encoding = "utf-8" ) as f :
145138 f .write (html_template )
139+
140+ # If output format is .html, copy to output directory
141+ if output_format == ".html" :
142+ if output_dir :
143+ setup_directory (output_dir )
144+ shutil .copy (temp_html_path , final_output_path )
145+ else :
146+ shutil .copy (temp_html_path , final_output_path )
147+
148+ # If output format is .pdf, delegate to PDF converter
149+ elif output_format == ".pdf" :
150+ if output_dir :
151+ setup_directory (output_dir )
152+ # Here, you'd use `temp_html_path` for PDF conversion
153+ # and save the PDF to `final_output_path`
154+ pass # Replace with PDF conversion logic
155+
156+ return final_output_path # Optional: return for chaining or logging
0 commit comments