diff --git a/README.md b/README.md index 8134521..2aa2fca 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ BaGu Thesis是一个专精于将LaTeX论文转换到DOCX格式转换的工具。 本项目旨在解决在中国国内高校中普遍存在的一个问题:大多数学校只提供DOCX格式的论文模板,且提交时也只能接受DOCX格式的论文。学校并不认可LaTeX编写的文章,也大多不愿意接受由LaTeX编译来的PDF格式的文档。此外,许多教师也只接受DOCX格式的文档。他们美其名曰"规范化"和"方便给你们添加评价",实则是他们自己除了DOCX格式的文档以外,就不知道如何在其它文档格式(例如PDF)里添加注释。他们把一个本应专注于学术内容的评审过程,变成了迂腐的格式检查任务。本项目旨在帮助能够使用LaTeX编写论文的学生无痛地将他们的论文转换为DOCX格式,以应付这些无谓的要求。如果你所在的学校或导师接受LaTeX或PDF格式的论文,请优先使用这些格式(我知道的就有西安电子科技大学的XDUTS模板)。毕竟本项目只是为了应付现实而不得不做的妥协。 -项目中的**BaGu(八股)**一词也来源于此现象:在中国,许多对于论文的审查都浪费在格式检查这一隔靴搔痒的事情上。论文格式要严格按照学校规定:页边距多少厘米、字号多少磅、行距多少倍、标题格式如何等等。这些琐碎的格式要求就像八股文一样。说到底,这都是因为论文本身的内容实际上没什么好看的,大家能写出来什么东西大家自己都心知肚明。无非是东拼西凑、复制粘贴、文献堆砌。审查的人们也心知肚明,但他们必须假装认真地审阅论文,于是格式就成了最好的挑刺对象。 +项目中的 **BaGu(八股)** 一词也来源于此现象:在中国,许多对于论文的审查都浪费在格式检查这一隔靴搔痒的事情上。论文格式要严格按照学校规定:页边距多少厘米、字号多少磅、行距多少倍、标题格式如何等等。这些琐碎的格式要求就像八股文一样。说到底,这都是因为论文本身的内容实际上没什么好看的,大家能写出来什么东西大家自己都心知肚明。无非是东拼西凑、复制粘贴、文献堆砌。审查的人们也心知肚明,但他们必须假装认真地审阅论文,于是格式就成了最好的挑刺对象。 本项目的存在,就是为了让你能够用LaTeX专注于内容创作,然后用最短的时间生成一个符合格式要求的DOCX文件,把时间浪费在更有意义的事情上:比如思考论文是否真的有意义,或者干脆出去玩一会儿。 diff --git a/docs/PROMPT.txt b/docs/PROMPT.txt new file mode 100644 index 0000000..680f2dd --- /dev/null +++ b/docs/PROMPT.txt @@ -0,0 +1,99 @@ +我正在创建一个用于将latex格式论文转换为docx格式论文的中间格式。该中间格式基于XML。我需要你创建@docs/XThesis.xsd 文件,使用XML Schema格式来描述这个中间格式。此外,你还需要创建@docs/XThesis.md 在该文件中,你需要引导用户访问XThesis.xsd文件,并且将一些不能在XSD中进行表述的内容写在此处。 + +以下是该中间格式的格式要求,请仔细分析各个元素之间的关系后再编写XSD文件,因为我不一定按层级顺序进行叙述。如果有不懂的地方请问我,而不是进行猜测: + +XML文档的根元素为article,表示为一个完整的文档。article元素下只允许4种子元素:preface,document,appendix,reference。四个元素必须按顺序出现,且有且只能有一个。 + +preface,document,appendix元素表示的意图不同。preface表示如摘要等内容,document表示文章的正文,appendix则表示附录,例如致谢,附录代码等。 + +reference元素的inner text是指向参考文献bib文件的路径。 + +有9个层级的章节元素,分别为section1,section2到section9。这些元素只能是document的子元素。章节元素具有一个名为caption的attribute,表示该章节的标题。元素的inner xml表示该章节的内容。章节元素的嵌套有规定,只能相邻向下嵌套,即只能section2在section1内,不能section2在section1内,也不能section3在section2内。section1只能是document的子节点。 + +有专用于preface和appendix的section元素(不带有任何数字后缀的)。它只能是preface和appendix的子节点,不能是document的子节点。section元素具有一个名为caption的attribute,表示该章节的标题。元素的inner xml表示该章节的内容。论文中的摘要,致谢等会被统一规划为这种section元素,不做区分。 + +有paragraph元素,表示一个自然段。paragraph只能是9种章节元素的子节点,或section的子节点。paragraph元素的inner xml表示当前自然段的内容。 + +有span元素,表示纯文本。span元素只能是paragraph的子对象。span元素的inner text是纯文本的内容。 + +有inlineeq元素,表示内联公式。inlineeq元素只能是paragraph的子对象。inlineeq元素的inner text是公式的内容(不包含两侧的$标记等,以latex原始代码呈现) + +有b,i元素,分别表示加粗和斜体文本。他们俩之间不能互相嵌套,也就是一个文本要么是加粗,要么是斜体,不能拥有多个样式。他们只能是paragraph的子元素。他们的inner text是需要进行特殊样式处理的文本。 + +有equation元素,表示行间公式。equation元素具有一个名为label的attribute,表示该公式的唯一引用名称。equation元素的inner text是公式的内容(以latex原始代码呈现)。equation元素只能是9种章节元素的子节点。 + +有figure元素,表示图片。figure元素具有一个名为label的attribute,表示该图片的唯一引用名称。figure元素具有一个名为caption的attribute,表示该图片的图题。figure元素的inner text是指向被引图片的路径。figure元素只能是9种章节元素的子节点。 + +有table元素,表示表格。table元素具有一个名为label的attribute,表示该表格的唯一引用名称。table元素具有一个名为caption的attribute,表示该表格的表题。table元素只能是9种章节元素的子节点。table元素的内部有且只能有一个thead和tbody元素。 +thead元素表示表格表头。thead元素的内部可以有多个td元素。 +tbody元素表示表格的内容。tbody元素的内部可以有多个tr元素,每个tr元素内有多个td元素。 +td元素是表格单元格的内容,其inner text是表格单元格中显示的文本。 +这些与HTML中的表格类似,但没有HTML表格中的任何样式设定,例如跨列,边框设置等。只是单纯的表示数据内容。这也就意味着thead中td的元素,和tbody中每个tr内的td元素数量需要完全一致。 + +有ref元素和cref元素。这两个元素都表示对内容的引用。他们的inner text均为需要引用的对象的名称,且使用逗号分割。这两个元素只能是paragraph的子对象。 + +有cite元素,表示对文献的引用。元素的inner text均为需要引用的文献的名称,且使用逗号分割。cite元素只能是paragraph的子对象。 + +有pagebreaker元素,表示在当前位置分页。该元素只能是9种章节元素的子节点,或section的子节点。 + + + + + +我正在编写一个将LaTeX论文,经由一个中间格式xthesis(基于XML),转换为docx论文的程序。我现在正在编写LaTeX到xthesis的部分,该部分被称为latex2xthesis。 + +# 四种映射模式 + +xthesis是一个结构化的XML格式,而LaTeX是一个线性文档,除少量environment语法以外,很难将其一对一映射到结构上。因此,在latex2xthesis中,我需要引入一套机制,将线性的latex文档转换到XML结构之上。 + +以下是我提出的四种映射模式: + +- 命令模式(command mode,可简称为cmd mode) + - 通过LaTeX命令来转化出一个XML元素。 + - 指定命令模式时,需要指定命令的名称name,同时还需要指定body内容。因为命令模式只是一条单独的语句,不像其他模式,有首尾两部分,可以框出来一个具体的范围。 + - 在该模式中,提供的可用变量为:`args`,表示该命令的参数,类型为`list[str]`。 +- 守卫模式(guard mode):通过两个LaTeX命令来框选出XML元素作用范围。头部的LaTeX命令会被消耗掉(consume),尾部的LaTeX命令不会被消耗。该模式适合于section,subsection这种分段式命令。 +- 注释模式(comment mode):通过两个固定文本的注释,框出范围。首尾注释均会被消耗。 +- 环境模式(environment mode,可简写为env mode):利用LaTeX自带的environment语法,天然地框出XML元素作用范围。 + +# 配置文件 + +## 配置文件要求 + +此外,由于不同地方的论文会使用不同的模板和LaTeX语句来构建他们的论文,因此需要为latex2xthesis编写一个配置文件,以拓展latex2xthesis的泛用性,让他不只盯着特定的LaTeX语句来生成xthesis文件。该配置文件为toml格式。 + +该配置文件中有preface键,用于配置哪些内容会被归类为序言内容。preface键是一个列表,列表中的每一种映射模式均会被视为可被转换为序言。在示例中,以环境模式为例,环境名为abstract,环境的参数0被作为标题 + +该配置文件中有document键 + +该配置文件中有appendix键 + +- 该配置文件中有reference键,用于配置参考文献内容。 + - 下有一个mode子健 + - 描述了如何抽取参考文献。 + - 下有一个inner子健 + - 用于表述reference键所关联的参考文献的路径 + - 该子键是一个python表达式 + - 可用变量为mode中抽取而出的变量 + - 该表达式需要返回一个字符串 + +## 你要做的工作 + +创建@docs/latex2xthesis.example.toml 文件,作为一个示例配置文件。你还要在其中使用TOML注释来解释每个字段的作用。 + +创建@src/config/latex2xthesis.py 文件。在该文件中,你需要利用Python自带的tomllib库,以及pydantic库,来实现对配置文件的读取和验证。封装一个函数,接受配置文件的路径输入(pathlib.Path),将TOML文件读取为一系列Python结构。 + + + + + +# latex2xthesis工作流程 + +接下来我将向你描述latex2xthesis的工作流程,请阅读以下工作流程,完成@src/latex2xthesis.py 文件的编写。 + +可使用的依赖: + +- 命令行解析功能可以从@src/cli/latex2xthesis.py 中获取 +- XThesis格式的组建和写出可以从@src/xthesis.py 中获取 +- 配置文件的读取和验证可以从@src/config/latex2xthesis.py 中获取 +- 使用pylatexenc库进行latex格式解析(我已经帮你添加好了,不需要再添加了) diff --git a/src/baguthesis.py b/src/baguthesis.py index 866b90e..c249bab 100644 --- a/src/baguthesis.py +++ b/src/baguthesis.py @@ -1,7 +1,8 @@ -import cli +import cli.baguthesis + def main(): - args = cli.parse_latex2docx_cli() + args = cli.baguthesis.parse_cli() if __name__ == "__main__": diff --git a/src/cli.py b/src/cli.py deleted file mode 100644 index b59a6bc..0000000 --- a/src/cli.py +++ /dev/null @@ -1,246 +0,0 @@ -import argparse -from dataclasses import dataclass -from pathlib import Path - - -@dataclass(frozen=True) -class LaTeX2XThesisCli: - """CLI arguments for LaTeX2XThesis""" - - input_file: Path - """The path to input LaTeX file""" - output_file: Path - """The path to output XThesis file""" - config_file: Path - """The path to LaTeX2XThesis configuration file""" - resource_dir: Path - """The path to resource directory for finding images and other resources referred in LaTeX file""" - - -def parse_latex2xthesis_cli() -> LaTeX2XThesisCli: - """Parse CLI arguments for LaTeX2XThesis""" - - parser = argparse.ArgumentParser( - prog="LaTeX to XThesis Converter", - description="Convert your LaTeX thesis into XThesis intermediate format.", - ) - parser.add_argument( - "-i", - "--input", - dest="input_file", - action="store", - type=Path, - required=True, - help="The path to input LaTeX file", - metavar="INPUT.tex", - ) - parser.add_argument( - "-o", - "--output", - dest="output_file", - action="store", - type=Path, - required=True, - help="The path to output XThesis file", - metavar="OUTPUT.xml", - ) - parser.add_argument( - "-c", - "--config", - dest="config_file", - action="store", - type=Path, - required=True, - help="The path to LaTeX2XThesis configuration file", - metavar="CONFIG.toml", - ) - parser.add_argument( - "-r", - "--resource-dir", - dest="resource_dir", - action="store", - type=Path, - required=True, - help="The path to resource directory for finding images and other resources referred in LaTeX file", - metavar="RESOURCE_DIR", - ) - - args = parser.parse_args() - return LaTeX2XThesisCli( - args.input_file, args.output_file, args.config_file, args.resource_dir - ) - - -@dataclass(frozen=True) -class XThesis2DocxCli: - """CLI arguments for XThesis2Docx""" - - input_file: Path - """The path to input XThesis file""" - output_file: Path - """The path to output Docx file""" - config_file: Path - """The path to XThesis2Docx configuration file""" - resource_dir: Path - """The path to resource directory for finding images and other resources referred in XThesis file""" - - -def parse_xthesis2docx_cli() -> XThesis2DocxCli: - """Parse CLI arguments for XThesis2Docx""" - - parser = argparse.ArgumentParser( - prog="XThesis to DOCX Converter", - description="Convert your XThesis intermediate format into DOCX.", - ) - parser.add_argument( - "-i", - "--input", - dest="input_file", - action="store", - type=Path, - required=True, - help="The path to input XThesis file", - metavar="INPUT.xml", - ) - parser.add_argument( - "-o", - "--output", - dest="output_file", - action="store", - type=Path, - required=True, - help="The path to output Docx file", - metavar="OUTPUT.docx", - ) - parser.add_argument( - "-c", - "--config", - dest="config_file", - action="store", - type=Path, - required=True, - help="The path to XThesis2Docx configuration file", - metavar="CONFIG.toml", - ) - parser.add_argument( - "-r", - "--resource-dir", - dest="resource_dir", - action="store", - type=Path, - required=True, - help="The path to resource directory for finding images and other resources referred in XThesis file", - metavar="RESOURCE_DIR", - ) - - args = parser.parse_args() - return XThesis2DocxCli( - args.input_file, args.output_file, args.config_file, args.resource_dir - ) - - -@dataclass(frozen=True) -class LaTeX2DocxCli: - """CLI arguments for LaTeX2Docx""" - - input_file: Path - """The path to input LaTeX file""" - output_file: Path - """The path to output Docx file""" - frontend_config_file: Path - """The path to LaTeX2XThesis configuration file""" - backend_config_file: Path - """The path to XThesis2Docx configuration file""" - resource_dir: Path - """The path to resource directory for finding images and other resources referred in file""" - - def break_into( - self, intermediate_file: Path - ) -> tuple[LaTeX2XThesisCli, XThesis2DocxCli]: - """ - Break this config into LaTeX2XThesis and XThesis2Docx configs - - :param intermediate_file: The path to intermediate XThesis file - :return: A tuple of broken LaTeX2XThesis and XThesis2Docx configs - """ - return ( - LaTeX2XThesisCli( - self.input_file, - intermediate_file, - self.frontend_config_file, - self.resource_dir, - ), - XThesis2DocxCli( - intermediate_file, - self.output_file, - self.backend_config_file, - self.resource_dir, - ), - ) - - -def parse_latex2docx_cli() -> LaTeX2DocxCli: - """Parse CLI arguments for LaTeX2Docx""" - parser = argparse.ArgumentParser( - prog="LaTeX to DOCX Converter", - description="Convert your LaTeX thesis into DOCX.", - ) - parser.add_argument( - "-i", - "--input", - dest="input_file", - action="store", - type=Path, - required=True, - help="The path to input LaTeX file", - metavar="INPUT.tex", - ) - parser.add_argument( - "-o", - "--output", - dest="output_file", - action="store", - type=Path, - required=True, - help="The path to output Docx file", - metavar="OUTPUT.docx", - ) - parser.add_argument( - "-f", - "--frontend-config", - dest="frontend_config_file", - action="store", - type=Path, - required=True, - help="The path to LaTeX2XThesis configuration file", - metavar="FRONTEND-CONFIG.toml", - ) - parser.add_argument( - "-b", - "--backend-config", - dest="backend_config_file", - action="store", - type=Path, - required=True, - help="The path to XThesis2Docx configuration file", - metavar="BACKEND-CONFIG.toml", - ) - parser.add_argument( - "-r", - "--resource-dir", - dest="resource_dir", - action="store", - type=Path, - required=True, - help="The path to resource directory for finding images and other resources referred in file", - metavar="RESOURCE_DIR", - ) - - args = parser.parse_args() - return LaTeX2DocxCli( - args.input_file, - args.output_file, - args.frontend_config_file, - args.backend_config_file, - args.resource_dir, - ) diff --git a/src/cli/__init__.py b/src/cli/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/cli/baguthesis.py b/src/cli/baguthesis.py new file mode 100644 index 0000000..0f3b5f2 --- /dev/null +++ b/src/cli/baguthesis.py @@ -0,0 +1,112 @@ +import argparse +from dataclasses import dataclass +from pathlib import Path +from .latex2xthesis import LaTeX2XThesisCli +from .xthesis2docx import XThesis2DocxCli + + +@dataclass(frozen=True) +class LaTeX2DocxCli: + """CLI arguments for LaTeX2Docx (BaGuThesis)""" + + input_file: Path + """The path to input LaTeX file""" + output_file: Path + """The path to output Docx file""" + frontend_config_file: Path + """The path to LaTeX2XThesis configuration file""" + backend_config_file: Path + """The path to XThesis2Docx configuration file""" + resource_dir: Path + """The path to resource directory for finding images and other resources referred in file""" + + def break_into( + self, intermediate_file: Path + ) -> tuple[LaTeX2XThesisCli, XThesis2DocxCli]: + """ + Break this config into LaTeX2XThesis and XThesis2Docx configs + + :param intermediate_file: The path to intermediate XThesis file + :return: A tuple of broken LaTeX2XThesis and XThesis2Docx configs + """ + return ( + LaTeX2XThesisCli( + self.input_file, + intermediate_file, + self.frontend_config_file, + self.resource_dir, + ), + XThesis2DocxCli( + intermediate_file, + self.output_file, + self.backend_config_file, + self.resource_dir, + ), + ) + + +def parse_cli() -> LaTeX2DocxCli: + """Parse CLI arguments for LaTeX2Docx (BaGuThesis)""" + parser = argparse.ArgumentParser( + prog="LaTeX to DOCX Converter", + description="Convert your LaTeX thesis into DOCX.", + ) + parser.add_argument( + "-i", + "--input", + dest="input_file", + action="store", + type=Path, + required=True, + help="The path to input LaTeX file", + metavar="INPUT.tex", + ) + parser.add_argument( + "-o", + "--output", + dest="output_file", + action="store", + type=Path, + required=True, + help="The path to output Docx file", + metavar="OUTPUT.docx", + ) + parser.add_argument( + "-f", + "--frontend-config", + dest="frontend_config_file", + action="store", + type=Path, + required=True, + help="The path to LaTeX2XThesis configuration file", + metavar="FRONTEND-CONFIG.toml", + ) + parser.add_argument( + "-b", + "--backend-config", + dest="backend_config_file", + action="store", + type=Path, + required=True, + help="The path to XThesis2Docx configuration file", + metavar="BACKEND-CONFIG.toml", + ) + parser.add_argument( + "-r", + "--resource-dir", + dest="resource_dir", + action="store", + type=Path, + required=True, + help="The path to resource directory for finding images and other resources referred in file", + metavar="RESOURCE_DIR", + ) + + args = parser.parse_args() + return LaTeX2DocxCli( + args.input_file, + args.output_file, + args.frontend_config_file, + args.backend_config_file, + args.resource_dir, + ) diff --git a/src/cli/latex2xthesis.py b/src/cli/latex2xthesis.py new file mode 100644 index 0000000..3162728 --- /dev/null +++ b/src/cli/latex2xthesis.py @@ -0,0 +1,72 @@ +import argparse +from dataclasses import dataclass +from pathlib import Path + + +@dataclass(frozen=True) +class LaTeX2XThesisCli: + """CLI arguments for LaTeX2XThesis""" + + input_file: Path + """The path to input LaTeX file""" + output_file: Path + """The path to output XThesis file""" + config_file: Path + """The path to LaTeX2XThesis configuration file""" + resource_dir: Path + """The path to resource directory for finding images and other resources referred in LaTeX file""" + + +def parse_cli() -> LaTeX2XThesisCli: + """Parse CLI arguments for LaTeX2XThesis""" + + parser = argparse.ArgumentParser( + prog="LaTeX to XThesis Converter", + description="Convert your LaTeX thesis into XThesis intermediate format.", + ) + parser.add_argument( + "-i", + "--input", + dest="input_file", + action="store", + type=Path, + required=True, + help="The path to input LaTeX file", + metavar="INPUT.tex", + ) + parser.add_argument( + "-o", + "--output", + dest="output_file", + action="store", + type=Path, + required=True, + help="The path to output XThesis file", + metavar="OUTPUT.xml", + ) + parser.add_argument( + "-c", + "--config", + dest="config_file", + action="store", + type=Path, + required=True, + help="The path to LaTeX2XThesis configuration file", + metavar="CONFIG.toml", + ) + parser.add_argument( + "-r", + "--resource-dir", + dest="resource_dir", + action="store", + type=Path, + required=True, + help="The path to resource directory for finding images and other resources referred in LaTeX file", + metavar="RESOURCE_DIR", + ) + + args = parser.parse_args() + return LaTeX2XThesisCli( + args.input_file, args.output_file, args.config_file, args.resource_dir + ) + diff --git a/src/cli/xthesis2docx.py b/src/cli/xthesis2docx.py new file mode 100644 index 0000000..b629d43 --- /dev/null +++ b/src/cli/xthesis2docx.py @@ -0,0 +1,72 @@ +import argparse +from dataclasses import dataclass +from pathlib import Path + + +@dataclass(frozen=True) +class XThesis2DocxCli: + """CLI arguments for XThesis2Docx""" + + input_file: Path + """The path to input XThesis file""" + output_file: Path + """The path to output Docx file""" + config_file: Path + """The path to XThesis2Docx configuration file""" + resource_dir: Path + """The path to resource directory for finding images and other resources referred in XThesis file""" + + +def parse_cli() -> XThesis2DocxCli: + """Parse CLI arguments for XThesis2Docx""" + + parser = argparse.ArgumentParser( + prog="XThesis to DOCX Converter", + description="Convert your XThesis intermediate format into DOCX.", + ) + parser.add_argument( + "-i", + "--input", + dest="input_file", + action="store", + type=Path, + required=True, + help="The path to input XThesis file", + metavar="INPUT.xml", + ) + parser.add_argument( + "-o", + "--output", + dest="output_file", + action="store", + type=Path, + required=True, + help="The path to output Docx file", + metavar="OUTPUT.docx", + ) + parser.add_argument( + "-c", + "--config", + dest="config_file", + action="store", + type=Path, + required=True, + help="The path to XThesis2Docx configuration file", + metavar="CONFIG.toml", + ) + parser.add_argument( + "-r", + "--resource-dir", + dest="resource_dir", + action="store", + type=Path, + required=True, + help="The path to resource directory for finding images and other resources referred in XThesis file", + metavar="RESOURCE_DIR", + ) + + args = parser.parse_args() + return XThesis2DocxCli( + args.input_file, args.output_file, args.config_file, args.resource_dir + ) + diff --git a/src/config/__init__.py b/src/config/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/latex2xthesis.py b/src/latex2xthesis.py index 85d100f..d9e312c 100644 --- a/src/latex2xthesis.py +++ b/src/latex2xthesis.py @@ -1,7 +1,8 @@ -import cli +import cli.latex2xthesis + def main(): - args = cli.parse_latex2xthesis_cli() + args = cli.latex2xthesis.parse_cli() if __name__ == "__main__": diff --git a/src/xthesis2docx.py b/src/xthesis2docx.py index 6ba9699..a8de4b0 100644 --- a/src/xthesis2docx.py +++ b/src/xthesis2docx.py @@ -1,7 +1,8 @@ -import cli +import cli.xthesis2docx + def main(): - args = cli.parse_xthesis2docx_cli() + args = cli.xthesis2docx.parse_cli() if __name__ == "__main__":