Skip to content

Commit 0d5c901

Browse files
author
Hodges
committed
add script to check latex documents to dev style guide
1 parent 275c3be commit 0d5c901

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

Manuals/scripts/check_manuals.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Created on Wed Mar 12 11:20:18 2025
4+
5+
@author: jhodges
6+
"""
7+
8+
import glob, os
9+
10+
def checkCaption(caption):
11+
12+
# Punctuation not allowed at the end of the short name
13+
captionTocDisallowedPunctuation = '.!?'
14+
15+
# Missing short name handling
16+
missing_toc_name_style = 1 # 0 - do not flag, 1 - flag as warning, 2 - flag as error
17+
18+
# Initialize outtxt
19+
outtxt = ''
20+
21+
# Check for presence of short name, needed for check for citation in TOC regardless
22+
if '[' not in caption.split('{')[0]:
23+
notoc_name = True
24+
short_name = ' '
25+
else:
26+
notoc_name = False
27+
short_name = caption.split('[')[1].split(']')[0]
28+
29+
if notoc_name and missing_toc_name_style == 1:
30+
outtxt = outtxt + "WARNING, %s caption, %s does not have a TOC name"%(file,caption) + "\n"
31+
elif notoc_name and missing_toc_name_style == 2:
32+
outtxt = outtxt + "ERROR, %s caption, %s does not have a TOC name"%(file,caption) + "\n"
33+
34+
# Check if short name ends in disallowed puncuation
35+
if short_name[-1] in captionTocDisallowedPunctuation:
36+
outtxt = outtxt + "WARNING, %s caption, %s TOC name ends in '%s'"%(file,caption, short_name[-1]) + "\n"
37+
38+
# Check if citation is included in the name used in TOC
39+
if notoc_name and '\\cite' in caption:
40+
outtxt = outtxt + "ERROR, %s citation in caption, %s"%(file,caption) + "\n"
41+
elif '\\cite' in short_name:
42+
outtxt = outtxt + "ERROR, %s citation in caption, %s"%(file,caption) + "\n"
43+
44+
return outtxt
45+
46+
def check_disallowed_commands(txt, file):
47+
disallowed_commands = ['\\bf{','\\tt{']
48+
outtxt = ''
49+
for cmd in disallowed_commands:
50+
split = txt.split(cmd)
51+
if len(split) > 1:
52+
for j in range(1, len(split)):
53+
line_count = len(split[j-1].split('\n'))+1
54+
outtxt = outtxt + "ERROR, %s %s located at line %d\n"%(file, cmd, line_count)
55+
return outtxt
56+
57+
texfiles = glob.glob('..'+os.sep+'*'+os.sep+'*.tex')
58+
59+
outtxt = '\n'
60+
for i in range(0, len(texfiles)):
61+
file = texfiles[i]
62+
63+
with open(file, 'r') as f:
64+
txt = f.read()
65+
66+
# Check figures
67+
figs = txt.split('begin{figure}')
68+
for j in range(1, len(figs)):
69+
fig = figs[j].split('end{figure}')[0]
70+
captions = fig.split('\\caption')
71+
for k in range(1, len(captions)):
72+
caption = captions[k].split('}')[0] + '}'
73+
outtxt = outtxt + checkCaption(caption)
74+
75+
# Check tables
76+
tabs = txt.split('begin{table}')
77+
for j in range(1, len(tabs)):
78+
tab = tabs[j].split('end{table}')[0]
79+
captions = tab.split('\\caption')
80+
for k in range(1, len(captions)):
81+
caption = captions[k].split('}')[0]
82+
outtxt = outtxt + checkCaption(caption)
83+
84+
# Check disallowed commands
85+
outtxt = outtxt + check_disallowed_commands(txt, file)
86+
87+
if len(outtxt) > 1:
88+
print("Warnings identified in the manual check:")
89+
print(outtxt)
90+
91+
with open('check_output.txt', 'w') as f:
92+
f.write(outtxt)

0 commit comments

Comments
 (0)