Module pygroff.compiler
23 | 27 | Expand source code 28 |
29 |import subprocess
30 | from pathlib import Path
31 | from pygroff.processor import *
32 |
33 | """
34 | This is the main module which calls all the required functions.
35 | """
36 |
37 |
38 | def clean_up(ag, tempfile, tempfile2, outfile):
39 | """
40 | Delete unneeded files
41 | Convert to word document if needed
42 | """
43 | if ag.d == True: # For debugging, delete or not delete files
44 | subprocess.run(f"rm {str(tempfile)}", shell=True)
45 | if ag.i == True:
46 | subprocess.run(f"rm {str(tempfile2)}", shell=True)
47 | if ag.w == True:
48 | subprocess.run(
49 | f"libreoffice --headless --convert-to docx --infilter='writer_pdf_import' {str(outfile)}",
50 | shell=True,
51 | )
52 |
53 |
54 | def decide_image(ag, tempfile, tempfile2, outfile):
55 | """
56 | Decide if the file has images. Default is true. Not much difference, except more images = more compile time
57 | """
58 | if ag.i == False: # If there are images
59 | subprocess.run(
60 | f"tbl {str(tempfile)}|groff -e -mspdf -Tpdf > {outfile}", shell=True
61 | )
62 | else: # Save compile time
63 | subprocess.run(
64 | f"tbl {str(tempfile)} | groff -e -mspdf -Tps > {str(tempfile2)} && ps2pdf {str(tempfile2)} {outfile}",
65 | shell=True,
66 | )
67 | if ag.toc == True:
68 | if ag.c == True:
69 | subprocess.run(
70 | f"pdftk {str(outfile)} cat 1 end 2-r2 output temp-{str(outfile)}",
71 | shell=True,
72 | )
73 | else:
74 | subprocess.run(
75 | f"pdftk {str(outfile)} cat end 1-r2 output temp-{str(outfile)}",
76 | shell=True,
77 | )
78 | Path.unlink(Path(str(outfile)))
79 | Path.rename(Path("temp-" + str(outfile)), outfile)
80 |
81 |
82 | def main(ag):
83 | """
84 | This calls the required functions and cleans up after the program is done
85 | """
86 | fpath = Path(ag.f)
87 | outfile = Path(ag.f).parent / ag.o
88 | with open(fpath.with_suffix(".ms"), "w+") as f:
89 | if ag.c == True: # Add cover
90 | f.write(
91 | f".ad c\n.tp\n.sp 5\n.(c\n{ag.t}\n.)c\n.sp 2\n.(c\n{ag.n}\n.)c\n.sp 2\n.(c\n{get_date(ag.df)}\n.)c\n.bp\n.ad l\n"
92 | )
93 | f.flush()
94 | f.write(intermediary_creator(fpath=fpath, ag=ag))
95 |
96 | if ag.toc == True: # Add table of contents
97 | f.write("\n.TC\n")
98 |
99 | tempfile, tempfile2 = fpath.with_suffix(".ms"), fpath.with_suffix(".ps")
100 | decide_image(ag, tempfile, tempfile2, outfile)
101 | clean_up(ag, tempfile, tempfile2, outfile)
102 |
103 | print(f"Done writing the file to -> {outfile}")
104 | Functions
112 |-
113 |
114 | def clean_up(ag, tempfile, tempfile2, outfile) 115 |
116 | -
117 | 119 |
Delete unneeded files 118 | Convert to word document if needed
120 |138 |121 | Expand source code 122 |
123 |
137 |def clean_up(ag, tempfile, tempfile2, outfile): 124 | """ 125 | Delete unneeded files 126 | Convert to word document if needed 127 | """ 128 | if ag.d == True: # For debugging, delete or not delete files 129 | subprocess.run(f"rm {str(tempfile)}", shell=True) 130 | if ag.i == True: 131 | subprocess.run(f"rm {str(tempfile2)}", shell=True) 132 | if ag.w == True: 133 | subprocess.run( 134 | f"libreoffice --headless --convert-to docx --infilter='writer_pdf_import' {str(outfile)}", 135 | shell=True, 136 | )
139 | 140 | def decide_image(ag, tempfile, tempfile2, outfile) 141 |
142 | -
143 | 144 |
Decide if the file has images. Default is true. Not much difference, except more images = more compile time
145 |175 |146 | Expand source code 147 |
148 |
174 |def decide_image(ag, tempfile, tempfile2, outfile): 149 | """ 150 | Decide if the file has images. Default is true. Not much difference, except more images = more compile time 151 | """ 152 | if ag.i == False: # If there are images 153 | subprocess.run( 154 | f"tbl {str(tempfile)}|groff -e -mspdf -Tpdf > {outfile}", shell=True 155 | ) 156 | else: # Save compile time 157 | subprocess.run( 158 | f"tbl {str(tempfile)} | groff -e -mspdf -Tps > {str(tempfile2)} && ps2pdf {str(tempfile2)} {outfile}", 159 | shell=True, 160 | ) 161 | if ag.toc == True: 162 | if ag.c == True: 163 | subprocess.run( 164 | f"pdftk {str(outfile)} cat 1 end 2-r2 output temp-{str(outfile)}", 165 | shell=True, 166 | ) 167 | else: 168 | subprocess.run( 169 | f"pdftk {str(outfile)} cat end 1-r2 output temp-{str(outfile)}", 170 | shell=True, 171 | ) 172 | Path.unlink(Path(str(outfile))) 173 | Path.rename(Path("temp-" + str(outfile)), outfile)
176 | 177 | def main(ag) 178 |
179 | -
180 | 181 |
This calls the required functions and cleans up after the program is done
182 |208 |183 | Expand source code 184 |
185 |
207 |def main(ag): 186 | """ 187 | This calls the required functions and cleans up after the program is done 188 | """ 189 | fpath = Path(ag.f) 190 | outfile = Path(ag.f).parent / ag.o 191 | with open(fpath.with_suffix(".ms"), "w+") as f: 192 | if ag.c == True: # Add cover 193 | f.write( 194 | f".ad c\n.tp\n.sp 5\n.(c\n{ag.t}\n.)c\n.sp 2\n.(c\n{ag.n}\n.)c\n.sp 2\n.(c\n{get_date(ag.df)}\n.)c\n.bp\n.ad l\n" 195 | ) 196 | f.flush() 197 | f.write(intermediary_creator(fpath=fpath, ag=ag)) 198 | 199 | if ag.toc == True: # Add table of contents 200 | f.write("\n.TC\n") 201 | 202 | tempfile, tempfile2 = fpath.with_suffix(".ms"), fpath.with_suffix(".ps") 203 | decide_image(ag, tempfile, tempfile2, outfile) 204 | clean_up(ag, tempfile, tempfile2, outfile) 205 | 206 | print(f"Done writing the file to -> {outfile}")
209 |