From e9e7b441b3806aa1d0bfa9064ebe1018cbdb637f Mon Sep 17 00:00:00 2001 From: Saifeddine ALOUI Date: Sat, 1 Jun 2024 22:16:14 +0200 Subject: [PATCH] ebnhanced writing --- lollms/functions/markdown2latex.py | 48 ++++++++++++++++++++++++++++-- lollms/functions/story_writing.py | 6 ++-- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/lollms/functions/markdown2latex.py b/lollms/functions/markdown2latex.py index e8a3712..8ed29ca 100644 --- a/lollms/functions/markdown2latex.py +++ b/lollms/functions/markdown2latex.py @@ -10,7 +10,21 @@ def markdown_to_latex(file_path: str) -> str: try: # Load the markdown file markdown_text = Path(file_path).read_text() - + + # Extract title, author, and prompted by information + title_match = re.search(r'^# (.*)', markdown_text, re.MULTILINE) + author_match = re.search(r'^Author: (.*)', markdown_text, re.MULTILINE) + prompted_by_match = re.search(r'^Prompted by: (.*)', markdown_text, re.MULTILINE) + + title = title_match.group(1) if title_match else "Untitled" + author = author_match.group(1) if author_match else "" + prompted_by = prompted_by_match.group(1) if prompted_by_match else "" + + # Remove the extracted parts from the markdown text + markdown_text = re.sub(r'^# .*\n', '', markdown_text, flags=re.MULTILINE) + markdown_text = re.sub(r'^Author: .*\n', '', markdown_text, flags=re.MULTILINE) + markdown_text = re.sub(r'^Prompted by: .*\n', '', markdown_text, flags=re.MULTILINE) + # Define conversion rules from markdown to LaTeX conversion_rules = [ (r'\\', r'\\textbackslash{}'), # Escape backslashes @@ -31,11 +45,35 @@ def markdown_to_latex(file_path: str) -> str: for pattern, replacement in conversion_rules: latex_text = re.sub(pattern, replacement, latex_text, flags=re.MULTILINE) + # Create the LaTeX document structure + latex_document = f""" + \\documentclass{{book}} + \\usepackage{{hyperref}} + \\usepackage{{graphicx}} + \\usepackage{{verbatim}} + + \\begin{{document}} + + \\title{{{title}}} + \\author{{{author}}} + \\date{{}} + + \\maketitle + + \\begin{{flushleft}} + \\textbf{{Prompted by:}} {prompted_by} + \\end{{flushleft}} + + {latex_text} + + \\end{{document}} + """ + # Define output file path output_path = Path(file_path).with_suffix('.tex') # Save the LaTeX text to a file - output_path.write_text(latex_text) + output_path.write_text(latex_document) # Finally we return the path to the LaTeX file return str(output_path) @@ -50,3 +88,9 @@ def markdown_to_latex_function(): "function_description": "Converts a markdown file to a LaTeX file.", # Description "function_parameters": [{"name": "file_path", "type": "str"}] # The set of parameters } + +if __name__ == "__main__": + # Test the function with a sample file + test_file_path = "story.md" + result = markdown_to_latex(test_file_path) + print(f"Generated LaTeX file: {result}") diff --git a/lollms/functions/story_writing.py b/lollms/functions/story_writing.py index 01ba28c..a700a99 100644 --- a/lollms/functions/story_writing.py +++ b/lollms/functions/story_writing.py @@ -70,13 +70,12 @@ def start_writing_story(prompt_ideas: str, llm: Any, story_file_path: str, build llm.step_start(f'Building section: {section["section_name"]}') section_name = section["section_name"] - section_description = section["section_description"] new_section = write_story_section( llm=llm, story_file_path=story_file_path, story_plan=story_plan, - current_section=section_description, + current_section=section_name, prompt_ideas=prompt_ideas ) @@ -139,6 +138,9 @@ def write_story_section(prompt_ideas: str, llm: Any, story_file_path: str, story # Initialize the story with the title and the first section new_section = f"# {story_title}\n\n" + new_section += f"Author: {llm.personality.name}\n" + new_section += f"Prompter: {llm.config.user_name}\n\n" + new_section += f"## {current_section}\n\n" prompt = "\n".join([