diff --git a/lollms/functions/markdown2latex.py b/lollms/functions/markdown2latex.py index c27aee4..e8a3712 100644 --- a/lollms/functions/markdown2latex.py +++ b/lollms/functions/markdown2latex.py @@ -1,42 +1,40 @@ -# Lollms function call definition file -# Here you need to import any necessary imports depending on the function requested by the user -import markdown +# Import necessary libraries +import re from pathlib import Path - -# Partial is useful if we need to preset some parameters from functools import partial - -# It is advised to import typing elements from typing import List - -# Import PackageManager if there are potential libraries that need to be installed -from lollms.utilities import PackageManager - -# ascii_colors offers advanced console coloring and bug tracing from ascii_colors import trace_exception -# Here is an example of how we install a non installed library using PackageManager -if not PackageManager.check_package_installed("markdown2latex"): - PackageManager.install_package("markdown2latex") - -# now we can import the library -import markdown2latex - -# Function to convert markdown file to LaTeX file -def markdown_file_to_latex(file_path: str) -> str: +# Define the conversion function +def markdown_to_latex(file_path: str) -> str: try: - # handle exceptions - # Load the markdown file markdown_text = Path(file_path).read_text() - - # Convert markdown to latex - latex_text = markdown2latex.convert(markdown_text) + # Define conversion rules from markdown to LaTeX + conversion_rules = [ + (r'\\', r'\\textbackslash{}'), # Escape backslashes + (r'([#]+) (.*)', lambda m: '\\' + 'sub'*(len(m.group(1))-1) + 'section{' + m.group(2) + '}'), # Convert headings + (r'\*\*(.*?)\*\*', r'\\textbf{\1}'), # Bold text + (r'\*(.*?)\*', r'\\textit{\1}'), # Italic text + (r'\!\[(.*?)\]\((.*?)\)', r'\\begin{figure}[h!]\n\\centering\n\\includegraphics[width=\\textwidth]{\2}\n\\caption{\1}\n\\end{figure}'), # Images + (r'\[(.*?)\]\((.*?)\)', r'\\href{\2}{\1}'), # Links + (r'`([^`]*)`', r'\\texttt{\1}'), # Inline code + (r'^```\s*([a-z]*)\s*\n([\s\S]*?)\n```', r'\\begin{verbatim}\2\\end{verbatim}'), # Code blocks + (r'^-\s+(.*)', r'\\begin{itemize}\n\\item \1\n\\end{itemize}'), # Unordered lists + (r'^\d+\.\s+(.*)', r'\\begin{enumerate}\n\\item \1\n\\end{enumerate}'), # Ordered lists + (r'^>(.*)', r'\\begin{quote}\1\\end{quote}'), # Block quotes + ] + + # Apply conversion rules + latex_text = markdown_text + for pattern, replacement in conversion_rules: + latex_text = re.sub(pattern, replacement, latex_text, flags=re.MULTILINE) + # Define output file path output_path = Path(file_path).with_suffix('.tex') - # Save the latex text to a file + # Save the LaTeX text to a file output_path.write_text(latex_text) # Finally we return the path to the LaTeX file @@ -44,33 +42,11 @@ def markdown_file_to_latex(file_path: str) -> str: except Exception as e: return trace_exception(e) -# Function to convert markdown string to LaTeX string -def markdown_string_to_latex(markdown_text: str) -> str: - try: - # handle exceptions - - # Convert markdown to latex - latex_text = markdown2latex.convert(markdown_text) - - # Finally we return the LaTeX text - return latex_text - except Exception as e: - return trace_exception(e) - -# Metadata function for markdown_file_to_latex -def markdown_file_to_latex_function(): +# Metadata function +def markdown_to_latex_function(): return { - "function_name": "markdown_file_to_latex", # The function name in string - "function": markdown_file_to_latex, # The function to be called - "function_description": "Converts a markdown file to a LaTeX file.", # Description - "function_parameters": [{"name": "file_path", "type": "str"}] # The set of parameters - } - -# Metadata function for markdown_string_to_latex -def markdown_string_to_latex_function(): - return { - "function_name": "markdown_string_to_latex", # The function name in string - "function": markdown_string_to_latex, # The function to be called - "function_description": "Converts a markdown string to a LaTeX string.", # Description - "function_parameters": [{"name": "markdown_text", "type": "str"}] # The set of parameters + "function_name": "markdown_to_latex", # The function name in string + "function": markdown_to_latex, # The function to be called + "function_description": "Converts a markdown file to a LaTeX file.", # Description + "function_parameters": [{"name": "file_path", "type": "str"}] # The set of parameters } diff --git a/lollms/functions/story_writing.py b/lollms/functions/story_writing.py index 34f995e..01ba28c 100644 --- a/lollms/functions/story_writing.py +++ b/lollms/functions/story_writing.py @@ -18,7 +18,7 @@ from pathlib import Path import json # Import markdown2latex -from lollms.functions.markdown2latex import markdown_file_to_latex +from lollms.functions.markdown2latex import markdown_to_latex from lollms.functions.file_manipulation import change_file_extension # Define the core functions @@ -86,9 +86,7 @@ def start_writing_story(prompt_ideas: str, llm: Any, story_file_path: str, build if build_latex: llm.step_start("Building latex file") import subprocess - latex = markdown_file_to_latex(story_file_path) - tex_file = Path(change_file_extension(story_file_path,".tex")) - tex_file.write_text(latex) + tex_file = markdown_to_latex(story_file_path) # Determine the pdflatex command based on the provided or default path if llm.config.pdf_latex_path: pdflatex_command = llm.config.pdf_latex_path @@ -156,7 +154,7 @@ def write_story_section(prompt_ideas: str, llm: Any, story_file_path: str, story f"{start_header_id_template}story_section_writer{end_header_id_template}" ] ) - new_section += llm.fast_gen(prompt) + new_section += llm.fast_gen(prompt, callback=llm.sink) # Write the new section to the story file story_path.write_text(new_section) @@ -186,7 +184,7 @@ def write_story_section(prompt_ideas: str, llm: Any, story_file_path: str, story ] ) new_section = f"## {current_section}\n\n" - new_section += llm.fast_gen(prompt).strip() + new_section += llm.fast_gen(prompt, callback=llm.sink).strip() # Append the new section to the story file story_path.write_text(story_content + "\n" + new_section)