1
0

refactor: change layout

This commit is contained in:
2026-05-26 13:30:04 +08:00
parent 43513c8960
commit b40cea95d0
11 changed files with 365 additions and 253 deletions

View File

@@ -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文件把时间浪费在更有意义的事情上比如思考论文是否真的有意义或者干脆出去玩一会儿。

99
docs/PROMPT.txt Normal file
View File

@@ -0,0 +1,99 @@
我正在创建一个用于将latex格式论文转换为docx格式论文的中间格式。该中间格式基于XML。我需要你创建@docs/XThesis.xsd 文件使用XML Schema格式来描述这个中间格式。此外你还需要创建@docs/XThesis.md 在该文件中你需要引导用户访问XThesis.xsd文件并且将一些不能在XSD中进行表述的内容写在此处。
以下是该中间格式的格式要求请仔细分析各个元素之间的关系后再编写XSD文件因为我不一定按层级顺序进行叙述。如果有不懂的地方请问我而不是进行猜测
XML文档的根元素为article表示为一个完整的文档。article元素下只允许4种子元素prefacedocumentappendixreference。四个元素必须按顺序出现且有且只能有一个。
prefacedocumentappendix元素表示的意图不同。preface表示如摘要等内容document表示文章的正文appendix则表示附录例如致谢附录代码等。
reference元素的inner text是指向参考文献bib文件的路径。
有9个层级的章节元素分别为section1section2到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原始代码呈现
有bi元素分别表示加粗和斜体文本。他们俩之间不能互相嵌套也就是一个文本要么是加粗要么是斜体不能拥有多个样式。他们只能是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命令不会被消耗。该模式适合于sectionsubsection这种分段式命令。
- 注释模式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格式解析我已经帮你添加好了不需要再添加了

View File

@@ -1,7 +1,8 @@
import cli
import cli.baguthesis
def main():
args = cli.parse_latex2docx_cli()
args = cli.baguthesis.parse_cli()
if __name__ == "__main__":

View File

@@ -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,
)

0
src/cli/__init__.py Normal file
View File

112
src/cli/baguthesis.py Normal file
View File

@@ -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,
)

72
src/cli/latex2xthesis.py Normal file
View File

@@ -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
)

72
src/cli/xthesis2docx.py Normal file
View File

@@ -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
)

0
src/config/__init__.py Normal file
View File

View File

@@ -1,7 +1,8 @@
import cli
import cli.latex2xthesis
def main():
args = cli.parse_latex2xthesis_cli()
args = cli.latex2xthesis.parse_cli()
if __name__ == "__main__":

View File

@@ -1,7 +1,8 @@
import cli
import cli.xthesis2docx
def main():
args = cli.parse_xthesis2docx_cli()
args = cli.xthesis2docx.parse_cli()
if __name__ == "__main__":