-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConversionScript.py
110 lines (100 loc) · 4.17 KB
/
ConversionScript.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
"""
Place this script and the FormattingSettings.tex file in the folder with
the .md files to convert. Then from the command line run this python script.
"""
import glob
import os
import pathlib
import subprocess
# List containing all files in folders and subfolders ordered by date created.
# From oldest to newest.
alldMDFilesPath = []
# Get path of current directory
workingDirPath = pathlib.Path().absolute()
# Get paths to subfolders in working directory.
subFoldersPath = [f.path for f in os.scandir(workingDirPath) if f.is_dir()]
# Oorder the subfolders by date created.
# subFoldersPath.sort(key=os.path.getctime)
# Oorder the subfolders by number at the beginning of the folder name.
subFoldersPath.sort(key=lambda x: int("".join([i for i in x if i.isdigit()])))
# Add file paths to a list. The files in the main folder/working directory are
# put together at the beginning. Then each folder.
# But first add the current folder/working directory.
# Path to find all .md files.
mainFolder = os.path.join(workingDirPath, "*.md")
# Get a list of the files in the folder.
MDfiles = glob.glob(mainFolder)
# Sort list by date created.
# MDfiles.sort(key=os.path.getctime)
# Sort list by number at the beginning of the file name.
MDfiles.sort(key=lambda x: int("".join([i for i in x if i.isdigit()])))
# Append files to master list.
alldMDFilesPath.extend(MDfiles)
# Now do the same for subfolders.
for dir in subFoldersPath:
# Create path to .md files.
subFolderFiles = os.path.join(dir, "*.md")
# Get a list of the files in the folder.
MDfiles = glob.glob(subFolderFiles)
# Order files in subdolder by date. Then add them to the master list
# together.
# MDfiles.sort(key=os.path.getctime)
# Sort list by number at the beginning of the file name.
MDfiles.sort(key=lambda x: int("".join([i for i in x if i.isdigit()])))
# Add files individually.
alldMDFilesPath.extend(MDfiles)
# Check that last line in all files is an empty line or add an empty line if
# needed.
for filePath in alldMDFilesPath:
# Open file to read and write.
file = open(filePath, "r+")
# Get the last line of the file.
lastLine = file.readlines()[-1]
# Check if the last line is an empty line.
# First assume last line is an empty line.
emptyLine = True
# Check every character in the last line.
for char in lastLine:
# Check character to see if it's just empty space.
if char != " ":
# If it's not an empty space, then it must be some character and
# the line is not empty line and the emptyLine variable will now
# reflect this. After the emptyLine variable has been set to False
# for the last line of file, there is no way to make it say
# otherwise for that file.
emptyLine = False
# If the emptyLine variable is false, then add an empty line to the file
# and close the file.
if emptyLine is False:
file.write("\n")
file.close()
# Create bash script to run pandoc command.
# First add all document paths to a single script.
# Variable to hold the string.
allPaths = " "
for filePath in alldMDFilesPath:
allPaths += "'" + filePath + "' "
bashScriptName = "ConversionScript.sh"
# Create file.
bashScript = open(bashScriptName, "w")
# Pandoc command used. Broken for readability.
# hard_line_breaks: Causes all newlines within a paragraph to be interpreted as
# hard line breaks instead of spaces. Basically, new lines are actually new
# lines. But I use Joplin's "soft breaks" checked because this way I make sure
# to use backslash for new lines. To not rely on hard breaks.
pandocCommand = (
"pandoc -s -V documentclass=article -V fontsize=12pt -f "
+ "markdown+lists_without_preceding_blankline+hard_line_breaks "
+ "--toc --include-in-header FormattingSettings.tex -o output.pdf"
)
# The pandoc command with the file paths.
fullCommand = pandocCommand + allPaths
# Append full command to bash script.
bashScript.write(fullCommand)
bashScript.close()
# Run command. Change working directory as well.
process = subprocess.Popen(
fullCommand, stdout=subprocess.PIPE, cwd=workingDirPath, shell=True
)
# Get output messages.
output, error = process.communicate()