Skip to content

Instantly share code, notes, and snippets.

@computational-sediment-hyd
Last active July 21, 2024 14:13
Show Gist options
  • Save computational-sediment-hyd/21c28dae7f7f5e6cb2c3ede7041c4ce0 to your computer and use it in GitHub Desktop.
Save computational-sediment-hyd/21c28dae7f7f5e6cb2c3ede7041c4ce0 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "b3b9bf4d",
"metadata": {},
"outputs": [],
"source": [
"import re\n",
"import sys\n",
"import os\n",
"\n",
"def work(name):\n",
" with open(name, encoding='UTF-8') as f:\n",
" lines = f.read()\n",
" \n",
" result = lines\n",
" \n",
" # コードブロックは処理をしないためコードブロックを除いた文字列を作成\n",
" resultNocode = re.sub(r'```.+?```', '', result, flags=re.DOTALL)\n",
" \n",
" words = re.findall(r'\\$.+?\\$', resultNocode)#, flags=re.DOTALL)\n",
"# print(words)\n",
" \n",
" for rin in words:\n",
" rout = rin\n",
" rout = re.sub(r'_', r'\\\\_' , rout)\n",
" rout = re.sub(r'\\^', r'\\\\^', rout)\n",
" rout = re.sub(r'\\*', r'\\*' , rout)\n",
" \n",
" rout = re.sub(r'^\\$', r'[tex:', rout)\n",
" rout = re.sub(r'\\$$', r']', rout)\n",
" \n",
" result = result.replace(rin, rout)\n",
" \n",
" # align環境の処理\n",
" words = re.findall(r'\\$\\$\\n\\\\begin{align}\\n.+?\\\\end{align}\\n\\$\\$\\n',resultNocode, flags=re.DOTALL)\n",
" \n",
" for rin in words:\n",
" rout = rin\n",
" for wesc in [r'\\[',r'\\]']:\n",
" rout = re.sub(wesc, wesc, rout)\n",
" \n",
" rout = re.sub(r'^\\$\\$\\n\\\\begin{align}\\n', r'<div align=\"center\">[tex:\\\\begin{align}\\n', rout)\n",
" rout = re.sub(r'\\\\end{align}\\n\\$\\$\\n$', r'\\\\end{align}]</div>\\n', rout)\n",
" \n",
" result = result.replace(rin, rout)\n",
" \n",
" \n",
" sname = os.path.splitext(name)\n",
" oname = sname[0] + '_output' + sname[-1]\n",
" with open(oname, mode=\"w\", encoding=\"UTF-8\") as f:\n",
" f.write(result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ef58c556",
"metadata": {},
"outputs": [],
"source": [
"print(sys.argv)\n",
"d = work(sys.argv[1])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.12"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment