提交 d8e5f740 编写于 作者: 陆巍

修改调整模板文件以及处理方式

上级 a35b8bd8
......@@ -45,15 +45,15 @@
% 把各种参数用\newcommand定义,以方便调试、替换。注意不要与已有命令冲突
% 定义的新命令中似乎只能使用字母。
% 在parameter标注内的内容将被lua程序单独取出来作为参数与默认值使用
% lua代码识别% parameter>>>>>到% <<<<<parameter之间的内容
% lua代码识别parameter>>>>>到<<<<<parameter之间的内容
% parameter>>>>>
\newcommand{\PGColor}{white}% 背景颜色
\newcommand{\TitleOneFont}{fzfangsong-z02s}% 标题1字体
\newcommand{\TitleOne}{安顺市西秀区}% 标题1内容
\newcommand{\TitleTwoFont}{zcoolwenyiti}% 标题2字体
\newcommand{\TitleTwo}{虹山湖公园}% 标题2内容
\newcommand{\IntroduceOneFont}{fzshusong-z01s}% 介绍1字体
\newcommand{\IntroduceOne}{虹山湖位于贵州省安顺市,是安顺人休闲的一个好地方。前身是一个人工湖,也叫虹山水库。2017年11月10日,批准为国家4A级旅游景区。}% 介绍1内容
\newcommand{\IntroOneFont}{fzshusong-z01s}% 介绍1字体
\newcommand{\IntroOne}{虹山湖位于贵州省安顺市,是安顺人休闲的一个好地方。前身是一个人工湖,也叫虹山水库。2017年11月10日,批准为国家4A级旅游景区。}% 介绍1内容
\newcommand{\CoverMainPic}{images/cover.png}% 封面主图片
\newcommand{\OrgLogoSize}{20mm}
\newcommand{\OrgLogo}{images/logo.png}
......@@ -62,8 +62,8 @@
\newcommand{\OrgOne}{某某某某}% 组织1内容
\newcommand{\OrgTwoFont}{zcoolwenyiti}% 组织2字体
\newcommand{\OrgTwo}{XXXXXX XXXXXX}% 组织2内容
\newcommand{\IntroduceTwoFont}{fzshusong-z01s}% 介绍2字体
\newcommand{\IntroduceTwo}{\\ XXXX:XXXXXXXXXXXX\\ XXXX:XXXXXXXX\\ XXXX:XXXXXXXXXXXXXX}% 介绍2内容
\newcommand{\IntroTwoFont}{fzshusong-z01s}% 介绍2字体
\newcommand{\IntroTwo}{\\ XXXX:XXXXXXXXXXXX\\ XXXX:XXXXXXXX\\ XXXX:XXXXXXXXXXXXXX}% 介绍2内容
% <<<<<parameter
......@@ -75,8 +75,8 @@
\begin{titlepage}
% 在module标注内的内容将被lua程序单独取出来作为模板使用
% lua代码识别% module>>>>>到% <<<<<module之间的内容
% module>>>>>
% lua代码识别% template>>>>>到% <<<<<template之间的内容
% template>>>>>
% 设置页面背景色
\pagecolor{\PGColor}
\begin{tikzpicture}[remember picture, overlay]
......@@ -90,7 +90,7 @@
};
% 介绍1
\node[below of = a, xshift = -60mm, yshift = -15mm, rectangle, text width=70mm, anchor = north west] {
\SetFont{\IntroduceOneFont}{10}{\parindent=-0.3em\indent \IntroduceOne}
\SetFont{\IntroOneFont}{10}{\parindent=-0.3em\indent \IntroOne}
};
% 默认读取images文件夹内的cover.png文件作为封面图片(宽高比21:11)
\node[rectangle, text width = 120mm, anchor = north west] at (-28mm, -60mm) {
......@@ -114,10 +114,10 @@
\color{black}
% 介绍2,字体为方正书宋体
\node[rectangle, text width=50mm, anchor = north west] at (120mm, -180mm) {
\SetFont{\IntroduceTwoFont}{10}{\IntroduceTwo}
\SetFont{\IntroTwoFont}{10}{\IntroTwo}
};
\end{tikzpicture}
% <<<<<module
% <<<<<template
\end{titlepage}
......
......@@ -46,15 +46,15 @@
% 把各种参数用\newcommand定义,以方便调试、替换。注意不要与已有命令冲突
% 定义的新命令中似乎只能使用字母。
% 在parameter标注内的内容将被lua程序单独取出来作为参数与默认值使用
% lua代码识别% parameter>>>>>到% <<<<<parameter之间的内容
% lua代码识别parameter>>>>>到<<<<<parameter之间的内容
% parameter>>>>>
\newcommand{\PGColor}{white}% 背景颜色
\newcommand{\TitleOneFont}{FZHei-B01S}% 标题1字体
\newcommand{\TitleOne}{安顺市西秀区}% 标题1内容
\newcommand{\TitleTwoFont}{fzfangsong-z02s}% 标题2字体
\newcommand{\TitleTwo}{虹山湖公园}% 标题2内容
\newcommand{\IntroduceOneFont}{fzshusong-z01s}% 介绍1字体
\newcommand{\IntroduceOne}{虹山湖位于贵州省安顺市,是安顺人休闲的一个好地方。前身是一个人工湖,也叫虹山水库。2017年11月10日,批准为国家4A级旅游景区。}% 介绍1内容
\newcommand{\IntroOneFont}{fzshusong-z01s}% 介绍1字体
\newcommand{\IntroOne}{虹山湖位于贵州省安顺市,是安顺人休闲的一个好地方。前身是一个人工湖,也叫虹山水库。2017年11月10日,批准为国家4A级旅游景区。}% 介绍1内容
\newcommand{\CoverMainPicSize}{335mm}% 封面主图片尺寸
\newcommand{\CoverMainPic}{images/cover.png}% 封面主图片
\newcommand{\OrgLogoSize}{14mm}
......@@ -64,8 +64,8 @@
\newcommand{\OrgOne}{某某某某}% 组织1内容
\newcommand{\OrgTwoFont}{zcoolwenyiti}% 组织2字体
\newcommand{\OrgTwo}{XXXXXX XXXXXX}% 组织2内容
\newcommand{\IntroduceTwoFont}{fzshusong-z01s}% 介绍2字体
\newcommand{\IntroduceTwo}{\\ XXXX:XXXXXXXXXXXX\\ XXXX:XXXXXXXX\\ XXXX:XXXXXXXXXXXXXX}% 介绍2内容
\newcommand{\IntroTwoFont}{fzshusong-z01s}% 介绍2字体
\newcommand{\IntroTwo}{\\ XXXX:XXXXXXXXXXXX\\ XXXX:XXXXXXXX\\ XXXX:XXXXXXXXXXXXXX}% 介绍2内容
\newcommand{\RemarkOne}{XXXX:XXXXXXXX}% 备注1内容
\newcommand{\RemarkTwo}{XXXX:XXXXXXXXXXXXXXXX}% 备注2内容
\newcommand{\RemarkThree}{XXXX:XXXXXXXXXXXXXXXX}% 备注3内容
......@@ -85,8 +85,8 @@
\begin{titlepage}
% 在module标注内的内容将被lua程序单独取出来作为模板使用
% lua代码识别% module>>>>>到% <<<<<module之间的内容
% module>>>>>
% lua代码识别% template>>>>>到% <<<<<template之间的内容
% template>>>>>
% 设置页面背景色
\pagecolor{\PGColor}
% tikzpicture后面中括号的参数让绘图时使用绝对坐标
......@@ -117,7 +117,7 @@
% 介绍1
\color{black}
\node[below of = b, xshift = -50mm, yshift = -20mm, text width = 120mm, anchor = north west] {
\SetFont{\IntroduceOneFont}{8}{\parindent=-0.4em\indent \IntroduceOne}
\SetFont{\IntroOneFont}{8}{\parindent=-0.4em\indent \IntroOne}
};
% 定义等腰三角形样式
\tikzset{
......@@ -147,7 +147,7 @@ rotate = 180, minimum size = 171mm, path picture = {
\node[right of = e, xshift = 50mm, text width = 60mm] {\RemarkTwo};
\node[right of = e, xshift = 120mm, text width = 60mm] {\RemarkThree};
\end{tikzpicture}
% <<<<<module
% <<<<<template
\end{titlepage}
......
......@@ -10,109 +10,97 @@
-- 定义CreateCover类
CreateCover = {
code = "",
number = "1"
}
function CreateCover:new(o, str)
function CreateCover:new(o, str, number)
o = o or {}
setmetatable(o, self)
setmetatable(o, self, self)
self.__index = self
self.code = str
self.number = number
return o
end
-- @brief 提取关键字内容
-- @param key 关键字
-- @param str 被提取字符串
-- @return 返回关键字对应内容
function GetValue(key, str, default)
local str1 = string.gsub(str, ".*,%s*" .. key .. "%s*=%s*", "")
if (str1 == str) then
--print(key .. "\n")
--print(str1 .. "\n\n")
return default
else
return string.gsub(str1, "%s*,.*", "")
end
-- @brief 记录日志
-- @param monitoring_data 监控数据
-- @param file 要保存的日志文件
-- @parm method 文件写入方式,0表示覆盖写入,1表示追加写入
function Logger(monitoring_data, file, method)
local str1 = monitoring_data .. "\n\n"
local meth = method == 0 and "w" or "a"
local log = io.open(file, meth)
log:write(str1)
log:close()
end
-- @brief 生成LaTeX封面代码
function CreateCover:Create()
-- 前后添加逗号,规范数据流格式,保证语句的适用性。
local user_input = ", " .. self.code .. ","
-- 获取封面编号(对于特殊字符需要加上%符号来匹配)
local cover_number = GetValue("cover%-number", user_input, "1")
-- 单独调试lua文件时的require路径与LaTeX调用时的路径是不一样的
--package.path = "fir/lua_tools/string.lua"
--require("FirString")
require("fir/lua_tools/string")
-- 为配合后面的工作,需要把用户输入的内容中的单引号换成双引号
-- 之所以用户输入时要用单引号,是因为双引号在LaTeX传入lua数据时另有别的意思
local user_input = string.gsub(self.code, "'", '"')
-- Logger(user_input, "test1.log", 0)
-- 读取模板文件内容
-- local fp = io.open("fir/create_cover/cover.tex", "r")
local fp = io.open("cover" .. cover_number .. ".tex", "r")
local fp = io.open("fir/create_cover/cover" .. self.number .. ".tex", "r")
local template = fp:read("*a")
fp:close()
--
print("cover" .. cover_number .. ".tex")
print(template)
do return end
-- 获取背景色
local pgcolor = GetValue("pgcolor", info, "white")
-- 获取标题1内容
local title1 = GetValue("title1", info, " ")
-- 获取标题1字体。默认为方正仿宋体
local title1_font = GetValue("title1%-font", info, "fzfangsong-z02s")
-- 获取标题2内容
local title2 = GetValue("title2", info, " ")
-- 获取标题2字体。默认为站酷文艺体
local title2_font = GetValue("title2%-font", info, "zcoolwenyiti")
-- 获取介绍1内容
local intro1 = GetValue("intro1", info, " ")
-- 获取介绍1字体。默认为方正书宋体
local intro1_font = GetValue("intro1%-font", info, "fzshusong-z01s")
-- 获取组织颜色
local org_color = GetValue("org%-color", info, "OliveGreen")
-- 获取组织1内容
local org1 = GetValue("org1", info, " ")
-- 获取组织1字体。默认为站酷文艺体
local org1_font = GetValue("org1%-font", info, "zcoolwenyiti")
-- 获取组织2内容
local org2 = GetValue("org2", info, " ")
-- 获取组织2字体。默认为站酷文艺体
local org2_font = GetValue("org2%-font", info, "zcoolwenyiti")
-- 获取组织2字体大小
local org2_size = GetValue("org2%-size", info, "18")
-- 获取介绍2内容
local intro2 = GetValue("intro2", info, " ")
-- 获取介绍2字体。默认为方正书宋体
local intro2_font = GetValue("intro2%-font", info, "fzshusong-z01s")
-- 读取create_cover.sty文件内容
local fp = io.open("fir/create_cover/cover.tex", "r")
-- local fp = io.open("cover.tex", "r")
local contents = fp:read("*a")
fp:close()
-- 读取模板内容中的参数块
local parm_block = string.match(template, "%% parameter>>>>>.*%% <<<<<parameter")
-- parm_block = string.gsub(parm_block, "\\newcommand{", "")
-- 定义并初始化参数列表二维数组。这里控制了支持的参数数量。
local parm_list = {}
for i = 0, 100 do
parm_list[i] = {}
for j = 0, 1 do
parm_list[i][j] = ""
end
end
-- 逐一读取参数块中的参数,并填入参数列表数组中。
local parm_number = FirString.Count(parm_block, "\\newcommand")
for i = 1, parm_number
do
local parm = FirString.SubString(parm_block, "\\newcommand{\\", "}", i)
parm_list[i][0] = parm
parm_list[i][1] = FirString.SubString(parm_block, "}{", "}", i)
-- print(i, parm_list[i][0] .. " " .. parm_list[i][1], "\n")
i = i + 1
end
-- 读取用户输入的参数并替换参数列表数组中的对应值
for i = 1, FirString.Count(user_input, ":")
do
local parm = FirString.GetKeyByOrder(user_input, i)
local value = FirString.GetValueByOrder(user_input, i)
--print(i, param, value)
for i = 1, parm_number
do
if (parm_list[i][0] == parm)
then
parm_list[i][1] = value
break
end
end
end
-- 提取模板文件中实际的模板内容
template = string.gsub(template, ".*%% template>>>>>", "")
template = string.gsub(template, "%% <<<<<template.*", "")
-- 把模板文件中对应的参数替换成实际值
for i = 1, parm_number
do
--print(parm_list[i][0], parm_list[i][1])
template = string.gsub(template, "\\" .. parm_list[i][0], parm_list[i][1])
end
-- 部分替换中会加入避免混淆的字符。
-- 例如pgcolor,如果不加入“=“,就会与“\pgcolor“混淆
contents = string.gsub(contents, "{pgcolor}", "{" .. pgcolor .. "}")-- 替换背景色
contents = string.gsub(contents, "{title1}", "{" .. title1 .. "}")-- 替换标题1
contents = string.gsub(contents, "title1%-font", title1_font)-- 替换标题1字体
contents = string.gsub(contents, "{title2}", "{" .. title2 .. "}")-- 替换标题2
contents = string.gsub(contents, "title2%-font", title2_font)-- 替换标题2字体
contents = string.gsub(contents, "{intro1}", "{" .. intro1 .. "}")-- 替换介绍1
contents = string.gsub(contents, "intro1%-font", intro1_font)-- 替换介绍1字体
contents = string.gsub(contents, "org%-color", org_color)-- 替换组织文字颜色
contents = string.gsub(contents, "{org1}", "{" .. org1 .. "}")-- 替换组织1
contents = string.gsub(contents, "org1%-font", org1_font)-- 替换组织1字体
contents = string.gsub(contents, "{org2}", "{" .. org2 .. "}")-- 替换组织2
contents = string.gsub(contents, "org2%-font", org2_font)-- 替换组织2字体
contents = string.gsub(contents, "org2%-size", org2_size)-- 替换组织2字体大小
contents = string.gsub(contents, "{intro2}", "{" .. intro2 .. "}")-- 替换介绍2
contents = string.gsub(contents, "intro2%-font", intro2_font)-- 替换介绍2字体
print(template)
-- 写入文件
local fp1 = io.open("cover_temp.tex", "w")
fp1:write(contents)
fp1:write(template)
fp1:close()
end
local cover = CreateCover:new(nil, "")
cover:Create()
--local cover = CreateCover:new(nil, '"TitleOne": "编译原理", "TitleTwo": "学习笔记", "OrgOne": "湖畔机器人", "OrgTwo": "LAKESIDE ROBOTICS"', "1")
--cover:Create()
\ No newline at end of file
......@@ -23,6 +23,7 @@
\RequirePackage{luacode}% lua支持
\RequirePackage{xcolor}
\RequirePackage{tikz}
\usetikzlibrary{shapes.geometric}
% ====================== 定义 ======================
......@@ -35,11 +36,11 @@
}
% 封面创建命令
\newcommand{\CreateCover}[1][]{
\newcommand{\CreateCover}[2][]{
\luaexec{
require("fir/create_cover/create_cover")
local cover = CreateCover:new(nil, "#1")
local cover = CreateCover:new(nil, "#1", "#2")
tex.sprint(cover:Create())
}
\include{cover_temp}
\include{cover_temp}
}
\ No newline at end of file
--[[
@file string.lua
@brief lua字符串处理函数库
@copyright MIT
@author 陆巍
@version 1.0.0
@date 2021-03-05
--]]
-- 定义字符串处理模块
FirString = {}
-- @brief 统计指定字符串在字符串中出现的次数
-- @param str 原字符串
-- @param target_str 目标字符串
-- @return 返回次数
function FirString.Count(str, target_str)
local count = 0
for i in string.gmatch(str, target_str) do
count = count + 1
end
return count
end
-- @brief 读取指定字符串边界内的字符
-- @param str 被提取字符串
-- @param start_str 起始字符串
-- @param end_str 结束字符串
-- @param n 第几次出现
-- @return 返回查到的字符串
-- @note 返回字符串的左右空格将被清除
function FirString.SubString(str, start_str, end_str, n)
if (n <= 0)
then
return "Error: Parameter n is invalid."
end
str = string.gsub(str, ".-" .. start_str .. "%s*", "", n)
str = string.gsub(str, end_str .. ".*", "")
return str
end
-- @brief 查找某个字符串第n次出现的位置
-- @param str 被提取字符串
-- @param n 顺序号
-- @return 返回查到的位置
function FirString.FindByOrder(str, target_str, n)
local i = 0
local site = 0
while (i < n)
do
site = string.find(str, target_str, site + 1)
i = i + 1
end
return site
end
-- @brief 通过序号获取类JSON数据的键
-- @param str 被提取字符串
-- @param n 顺序号
-- @return 返回查到的键
function FirString.GetKeyByOrder(str, n)
if (n <= 0)
then
return "Error: Parameter n is invalid."
end
local site = FirString.FindByOrder(str, ":", n)
str = string.sub(str, 1, site)
str = string.gsub(str, '"%s*:', '')
str = string.gsub(str, '.*"', '')
return str
end
-- @brief 通过序号获取类JSON数据的值
-- @param str 被提取字符串
-- @param n 顺序号
-- @return 返回查到的值
function FirString.GetValueByOrder(str, n)
if (n <= 0)
then
return "Error: Parameter n is invalid."
end
local site = FirString.FindByOrder(str, ":", n)
str = string.sub(str, site)
str = string.gsub(str, ':%s*"', '')
str = string.gsub(str, '".*', '')
return str
end
return FirString
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册