{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始编译目标pdf文件:  临时文件/38_计数原理与排列组合.tex\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "import os,re,json,sys\n",
    "\n",
    "#范围定义在使用前需要替换\n",
    "\"\"\"使用前替换范围定义\"\"\"\n",
    "obj_range = \"K0810:K0817999\"\n",
    "\"\"\"范围定义到此结束\"\"\"\n",
    "#定义文件名\n",
    "\"\"\"规定文件名\"\"\"\n",
    "index = \"38\"\n",
    "title = \"计数原理与排列组合\"\n",
    "\"\"\"文件名到此结束\"\"\"\n",
    "\n",
    "filename = index+\"_\"+title\n",
    "outputfile = r\"临时文件/\"+filename+\".tex\"\n",
    "\n",
    "template_file = r\"模板文件/复习课目标模板.tex\"\n",
    "\n",
    "\n",
    "# 检查某一字符串是否在由,:的表达式给出的范围内\n",
    "def within_range(string,list):\n",
    "    flag = False\n",
    "    for item in list:\n",
    "        if string == item.strip():\n",
    "            flag = True\n",
    "            break\n",
    "        elif \":\" in item:\n",
    "            start, end = item.split(\":\")\n",
    "            if start <= string <= end:\n",
    "                flag = True\n",
    "                break\n",
    "    return flag\n",
    "\n",
    "# 读取课时目标数据库\n",
    "with open(r\"../题库0.3/LessonObj.json\",\"r\",encoding = \"utf8\") as f:\n",
    "    database = f.read()\n",
    "obj_dict = json.loads(database)\n",
    "\n",
    "# 根据范围生成若干用于检查的闭区间范围\n",
    "obj_range_list = obj_range.split(\",\")\n",
    "output_string = \"\"\n",
    "\n",
    "# 逐一选择目标, 并整合成表格的内容部分\n",
    "for obj_id in obj_dict:\n",
    "    if within_range(obj_id,obj_range_list):\n",
    "        output_string += obj_id + \" & \" + obj_dict[obj_id][\"content\"] + \" & \" + r\"\\\\ \\hline\" + \"\\n\"\n",
    "\n",
    "# 打开模板文件\n",
    "with open(template_file,\"r\",encoding=\"utf8\") as f:\n",
    "    latex_raw = f.read()\n",
    "\n",
    "#识别操作系统\n",
    "if sys.platform != \"win32\":\n",
    "    latex_raw = re.sub(r\"fontset[\\s]*=[\\s]*windows\",\"fontset = fandol\",latex_raw)\n",
    "    latex_raw = re.sub(r\"\\\\setCJKmainfont\",r\"% \\\\setCJKmainfont\",latex_raw)\n",
    "\n",
    "#预处理\n",
    "exec_list = [(\"编号待替换\",index),(\"标题待替换\",title),(\"内容待替换\",output_string)]\n",
    "for command in exec_list:\n",
    "    latex_raw = latex_raw.replace(command[0],command[1])\n",
    "\n",
    "#输出到临时文件夹\n",
    "with open(outputfile,\"w\",encoding = \"utf8\") as f:\n",
    "    f.write(latex_raw)\n",
    "print(\"开始编译目标pdf文件: \", outputfile)\n",
    "os.system(\"xelatex -interaction=batchmode -output-directory=\" + \"临时文件\" + \" \"+ outputfile)\n",
    "print(os.system(\"xelatex -interaction=batchmode -output-directory=\" + \"临时文件\" + \" \"+ outputfile))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.8 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "d311ffef239beb3b8f3764271728f3972d7b090c974f8e972fcdeedf230299ac"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
