{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "开始编译教师版本pdf文件:  临时文件/题库_教师用_20230401.tex\n",
      "0\n",
      "开始编译学生版本pdf文件:  临时文件/题库_学生用_20230401.tex\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "import os,re,time,json,sys\n",
    "\n",
    "\"\"\"\n",
    "模板文件目录下 题目清单.tex 文件不能缺失\n",
    "\"\"\"\n",
    "\n",
    "\"\"\"---设置题目列表---\"\"\"\n",
    "#留空为编译全题库, a为读取临时文件中的题号筛选.txt文件生成题库\n",
    "problems = r\"\"\"\n",
    "a\n",
    "\"\"\"\n",
    "\"\"\"---设置题目列表结束---\"\"\"\n",
    "\n",
    "\"\"\"---设置文件名---\"\"\"\n",
    "#目录和文件的分隔务必用/\n",
    "filename = \"临时文件/题库\"\n",
    "\"\"\"---设置文件名结束---\"\"\"\n",
    "\n",
    "\"\"\"---设置是否需要解答题的空格---\"\"\"\n",
    "spaceflag = False\n",
    "\"\"\"---设置空格结束---\"\"\"\n",
    "\n",
    "#读取系统日期\n",
    "current_time = time.localtime()\n",
    "time_string = \"_\"+str(current_time.tm_year).zfill(4)+str(current_time.tm_mon).zfill(2)+str(current_time.tm_mday).zfill(2)\n",
    "\n",
    "teachers_latex_file = filename + \"_教师用\" + time_string + \".tex\"\n",
    "students_latex_file = filename + \"_学生用\" + time_string + \".tex\"\n",
    "\n",
    "#生成数码列表, 逗号分隔每个区块, 区块内部用:表示整数闭区间\n",
    "def generate_number_set(string,dict):\n",
    "    string = re.sub(r\"[\\n\\s]\",\"\",string)\n",
    "    string_list = string.split(\",\")\n",
    "    numbers_list = []\n",
    "    for s in string_list:\n",
    "        if not \":\" in s:\n",
    "            numbers_list.append(s.zfill(6))\n",
    "        else:\n",
    "            start,end = s.split(\":\")\n",
    "            for ind in range(int(start),int(end)+1):\n",
    "                numbers_list.append(str(ind).zfill(6))\n",
    "    return numbers_list\n",
    "\n",
    "#将正确率转化为含颜色代码的字符串\n",
    "def get_color(value):\n",
    "    value = float(value)\n",
    "    if value>=0.5:\n",
    "        (r,g,b)=(1,2-2*value,0)\n",
    "    else:\n",
    "        (r,g,b)=(2*value,1,0)\n",
    "    return \"{\" + \"%.3f\" %(r) + \",\" + \"%.3f\" %(g) + \",0}\"\n",
    "\n",
    "\n",
    "def color_value(matchobj):\n",
    "    value = matchobj.group(1)\n",
    "    return \"\\t\"+\"\\\\fcolorbox[rgb]{0,0,0}\"+ get_color(value) +\"{\" + value +\"}\"\n",
    "\n",
    "\n",
    "#读取题库json文件并转化为字典\n",
    "with open(r\"../题库0.3/Problems.json\",\"r\",encoding = \"utf8\") as f:\n",
    "    database = f.read()\n",
    "pro_dict = json.loads(database)\n",
    "\n",
    "#读取目标数据库json并转化为字典\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",
    "if problems.strip() == \"\":\n",
    "    problem_list = list(pro_dict.keys())\n",
    "elif problems.strip()[0] == \"a\":\n",
    "    with open(\"文本文件/题号筛选.txt\",\"r\",encoding = \"utf8\") as f:\n",
    "        problems = f.read()\n",
    "    problem_list = [id for id in generate_number_set(problems.strip(),pro_dict) if id in pro_dict]\n",
    "else:\n",
    "    problem_list = [id for id in generate_number_set(problems.strip(),pro_dict) if id in pro_dict]\n",
    "\n",
    "data_teachers = \"\"\n",
    "data_students = \"\"\n",
    "id_list = \"\"\n",
    "\n",
    "#生成教师题目字符串与学生题目字符串, 准备替换至latex文件\n",
    "for id in problem_list:\n",
    "    problemset = pro_dict[id]\n",
    "    problem = problemset[\"content\"]\n",
    "    solution = (problemset[\"solution\"] if problemset[\"solution\"] != \"\" else \"暂无解答与提示\")\n",
    "    answer = \"\\\\textcolor{red}{\" + (problemset[\"ans\"] if problemset[\"ans\"] != \"\" else \"暂无答案\") + \"}\"\n",
    "    remarks = (problemset[\"remark\"] if problemset[\"remark\"] != \"\" else \"暂无备注\")\n",
    "    usages_list = problemset[\"usages\"]\n",
    "    if len(usages_list) > 0:\n",
    "        usage = re.sub(\"\\\\t([\\d]\\.[\\d]{0,10})\",color_value,\"\\n\\n\".join(usages_list))\n",
    "        usage = re.sub(\"[\\\\t ]([\\d]\\.[\\d]{0,10})\",color_value,usage)\n",
    "    else:\n",
    "        usage = \"暂无使用记录\"\n",
    "    origin = (problemset[\"origin\"] if problemset[\"origin\"] != \"\" else \"出处不详\")\n",
    "    objects = problemset[\"objs\"]\n",
    "    if len(objects) == 0:\n",
    "        objects = \"暂未关联目标\\n\\n\"\n",
    "    elif \"KNONE\" in [o.upper() for o in objects]:\n",
    "        objects = \"该题的考查目标不在目前的集合中\\n\\n\"\n",
    "    else:\n",
    "        objects_string = \"\"\n",
    "        for obj in objects:\n",
    "            if not obj in obj_dict:\n",
    "                objects_string = \"目标\" + obj + \"有误\\n\\n\"\n",
    "                break\n",
    "            else:\n",
    "                objects_string += \"\\\\textcolor{blue}{\" + obj + \"|\" + obj_dict[obj][\"content\"] + \"}\\n\\n\"\n",
    "        objects = objects_string\n",
    "    space = (\"\" if problemset[\"space\"] == \"\" or spaceflag == False else \"\\n\"+r\"\\vspace*{\"+problemset[\"space\"]+\"}\\n\")\n",
    "    tags = (\"|\".join(problemset[\"tags\"]) if len(problemset[\"origin\"])>0 else \"暂无标签\")\n",
    "    raw_string = \"\\\\item \" + \"{\\\\tiny (\"+id+\")} \"+problem\n",
    "    teachers_string = raw_string.replace(\"\\\\tiny \",\"\")+\"\\n\\n关联目标:\\n\\n\"+ objects + \"\\n\\n标签: \" + tags + \"\\n\\n答案: \"+answer + \"\\n\\n\" + \"解答或提示: \" + solution + \"\\n\\n使用记录:\\n\\n\"+ usage + \"\\n\" + \"\\n\\n备注: \" + remarks + \"\\n\\n出处: \"+origin + \"\\n\"\n",
    "    students_string = raw_string + space + \"\\n\\n\"\n",
    "    data_teachers += teachers_string\n",
    "    data_students += students_string\n",
    "\n",
    "\n",
    "\n",
    "#替换latex文件的内容并编译\n",
    "with open(\"模板文件/题目清单.txt\",\"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]*none\",\"fontset = fandol\",latex_raw)\n",
    "    latex_raw = re.sub(r\"\\\\setCJKmainfont\",r\"% \\\\setCJKmainfont\",latex_raw)\n",
    "\n",
    "latex_raw = re.sub(r\"\\\\tableofcontents\",\"\",latex_raw)\n",
    "\n",
    "latex_teachers = latex_raw.replace(\"编译模板\",data_teachers)\n",
    "with open(teachers_latex_file,\"w\",encoding = \"utf8\") as f:\n",
    "    f.write(latex_teachers)\n",
    "print(\"开始编译教师版本pdf文件: \", teachers_latex_file)\n",
    "d = re.search(\"/[^/]*$\",teachers_latex_file).span()[0]\n",
    "pdf_dir = teachers_latex_file[:d]\n",
    "os.system(\"xelatex -interaction=batchmode -output-directory=\" + pdf_dir + \" \"+ teachers_latex_file)\n",
    "print(os.system(\"xelatex -interaction=batchmode -output-directory=\" + pdf_dir + \" \"+ teachers_latex_file))\n",
    "\n",
    "latex_students = latex_raw.replace(\"编译模板\",data_students)\n",
    "with open(students_latex_file,\"w\",encoding = \"utf8\") as f:\n",
    "    f.write(latex_students)\n",
    "print(\"开始编译学生版本pdf文件: \", students_latex_file)\n",
    "d = re.search(\"/[^/]*$\",students_latex_file).span()[0]\n",
    "pdf_dir = students_latex_file[:d]\n",
    "os.system(\"xelatex -interaction=batchmode -output-directory=\" + pdf_dir + \" \"+ students_latex_file)\n",
    "print(os.system(\"xelatex -interaction=batchmode -output-directory=\" + pdf_dir + \" \"+ students_latex_file))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mathdept",
   "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.9.15"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "ff3c292c316ba85de6f1ad75f19c731e79d694e741b6f515ec18f14996fe48dc"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
