/
src2pdf
136 lines (119 loc) · 3.62 KB
/
src2pdf
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/bin/bash -e
# Check everything is installed:
PYTHON_PATH=$(which python)
return_status_1=$(echo $? 2> /dev/null)
PIP_PATH=$(which pip 2> /dev/null)
return_status_2=$(echo $?)
#TEX_PATH=$(which texlive-latex-extra 2> /dev/null)
#return_status_3=$(echo $?)
sudo apt-get -y -qq install python python-pip texlive-latex-extra
pip install --user Pygments
tex_file=$1
# EDIT THESE VARIABLES!!!!
doc_title="My App Name"
author="Who Am I"
abstract=$(cat <<EOM
Source code + technical documentation
EOM
)
introduction=$(cat <<EOM
This document has the objective of listing the source code of the $doc_title application
EOM
)
declare -A file_types
# The language # File pattern # Exclude files
file_types[0]="php|-name '*.php'|! -name '*.blade.php'"
file_types[1]="html|-name '*.blade.php'|"
file_types[2]="c"
file_types[3]="cpp"
file_types[4]="basemake"
file_types[5]="make"
file_types[6]="cmake"
# DONE EDITING! :)
input_minted_header () {
tex_file=$1
cat <<EOF >$tex_file ## Print the tex file header
\documentclass{article}
\usepackage{minted}
\usepackage{url}
\urlstyle{same} % omit this command if you want monospaced-font look
\newcommand\purl[1]{\protect\url{#1}} % "protected url"
EOF
}
input_doc_header () {
tex_file=$1
cat <<EOF >>$tex_file
\begin{document}
\title{$doc_title}
\author{$author}
\maketitle
\newpage
\begin{abstract}
$abstract
\end{abstract}
\newpage
\tableofcontents
\newpage
\section{Introduction}
$introduction
\section{Source code}
EOF
}
input_section () {
newdirectory=$1
olddirectory=$2
if [ "$newdirectory" != "$olddirectory" ]; then
IFS='/' read -r -a structure <<< "$newdirectory"
IFS='/' read -r -a oldstructure <<< "$olddirectory"
for level in "${!structure[@]}"; do
if [ "${structure[$level]}" != "${oldstructure[$level]}" ]; then
case $level in
0) level_command="\subsection"
;;
1) level_command="\subsubsection"
;;
2) level_command="\paragraph"
;;
*) level_command="\subparagraph"
;;
esac
file_title=${structure[$level]}
echo "$level_command{\purl{/$file_title}}" >> $tex_file ## Create a section for each file
fi
done
fi
olddirectory=$newdirectory
}
input_listings () {
olddirectory="/"
for file in "${files_to_process[@]}"; do ## Loop through each file
file_title=$file
directory=$(dirname "${file}")
if [ -d "./${file}" ]; then
echo "Skipping ./${file}"
else
echo "\newpage" >> $tex_file ## start each section on a new page
input_section $directory $olddirectory
echo "\subparagraph{\purl{/$file_title}}" >> $tex_file ## Create a section for each file
echo "\inputminted[linenos]{$language}{$file}" >> $tex_file
fi
done
}
input_minted_header $tex_file
input_doc_header $tex_file
for i in "${!file_types[@]}"; do
IFS='|' read -r -a file_type <<< "${file_types[$i]}"
language=${file_type[0]}
files_to_include=${file_type[1]}
files_to_exclude=${file_type[2]}
# Get the list of files to process
cmd="find . -type d \( -name storage -o -name vendor \) -prune -o -type f $files_to_include \
! -regex '.*/\..*' ! -name '.*' $files_to_exclude ! -name '*~' ! -name 'src2pdf' \
| sed 's/^\..//'"
files_to_process=( $(eval $cmd) )
input_listings
done
echo "\end{document}" >> $tex_file
pdflatex -shell-escape $tex_file -output-directory . && pdflatex -shell-escape $tex_file -output-directory .
find . -name "$tex_file.*" ! -name "$tex_file.pdf" -type f -exec rm -f {} +
## This needs to be run twice for the TOC to be generated