Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Tissue Research JKU
Tissue Research Doc
Commits
64e85c74
Commit
64e85c74
authored
Nov 26, 2021
by
Sabina Köfler
Browse files
Create functions for everything
parent
b7efdd0a
Changes
1
Show whitespace changes
Inline
Side-by-side
quick-start-guides/convert_to_pdf.sh
View file @
64e85c74
#!/bin/bash
# Checks for the presence of markdown files
echo
md
=
$(
ls
-1
*
.md 2>/dev/null |
wc
-l
)
# ls -1 list one file per line
mdown
=
$(
ls
-1
*
.mdown 2>/dev/null |
wc
-l
)
mark
=
$(
ls
-1
*
.markdown 2>/dev/null |
wc
-l
)
present
=
$((
$md
+
$mdown
+
$mark
))
if
[
"
$present
"
-eq
0
]
;
then
# First replace all whitespaces from filenames with underscores
replace_whitespaces
()
{
for
f
in
*
\
*
;
do
mv
"
$f
"
"
${
f
// /_
}
"
;
done
}
# Create array to store all md files for later
# readarray -d '' array < <(find . -iname "*.md" -o -iname "*.mdown" -o -iname "*.markdown")
create_filearray
()
{
mapfile
-d
$'
\0
'
files < <
(
find
.
-iname
"*.md"
-print0
)
}
find_markdown
()
{
if
[
"
${#
files
[@]
}
"
-eq
0
]
;
then
echo
-e
"No markdown files detected!
\n
"
exit
fi
pdf_dir
=
"PDFs"
else
echo
-e
"
${#
files
[@]
}
markdown files detected!
\n
"
fi
}
# Checks for output directory and creates one if missing
if
[
!
-d
$pdf_dir
]
;
then
create_outdir_if_not_exists
()
{
if
[
!
-d
"
$pdf_dir
"
]
;
then
mkdir
$pdf_dir
echo
-e
"Output folder created.
\n
"
else
else
echo
-e
"Selecting existing output folder.
\n
"
fi
fi
}
# Asks user for the LaTeX engine to be used
echo
-e
"LaTeX engines
\n
------------
\n
"
PS3
=
$'
\n
'
"Please choose a LaTeX engine: "
select
option
in
pdflatex lualatex xelatex
do
select_latex_engine
()
{
echo
-e
"LaTeX engines
\n
------------
\n
"
PS3
=
$'
\n
'
"Please choose a LaTeX engine (type 1 for pdflatex): "
select
option
in
pdflatex lualatex xelatex
do
case
$option
in
pdflatex
)
echo
-e
"
\n
pdflatex engine selected"
;;
...
...
@@ -35,44 +48,69 @@ do
echo
"Please choose an option"
;;
esac
break
done
echo
done
echo
}
# Allows user to add custom LaTeX variables
echo
-n
"Would you like to add any LaTeX details(e.g. --variable mainfont:
\"
Gill Sans
\"
)?: "
read
variable
extract_filename
()
{
y
=
"
$file
"
# ./FILENAME.EXTENSION
x
=
"
${
y
%.*
}
"
# ./FILENAME
z
=
"
${
x
##*/
}
"
# FILENAME
echo
"Extracted filename:
${
z
}
"
}
# Compiles documents
echo
-e
"
\n
Beginning conversion...
\n
"
count
=
0
find
.
-iname
"*.md"
-o
-iname
"*.mdown"
-o
-iname
"*.markdown"
|
while
read
-r
i
;
do
# find -iname finds <pattern> case insensitive
y
=
"
$i
"
x
=
"
${
y
%.*
}
"
z
=
"
${
x
##*/
}
"
pdf_filename
=
"
$z
.pdf"
pdf_path
=
"./
${
pdf_dir
}
/
${
pdf_filename
}
"
created_date
=
$(
stat
-c
%y
$pdf_path
)
# Get file last modified date and time
create_date_string
()
{
created_date
=
$(
stat
-c
%y
${
pdf_path
}
)
# Get file last modified date and time
IFS
=
" "
read
-r
a b c
<<<
"
$created_date
"
# split the string into 3 parts (date time timezone_offset)
file_date
=
"
$a
"
# the first part is the date
ftime
=
"
${
b
%.*
}
"
# the second part is the time (but its too exact, so strip everything after the .)
file_time
=
${
ftime
//
:
}
# remove the colons from the time stamp
# echo "Constructed date string: $file_date and time string: $file_time"
}
construct_pdf_path
()
{
pdf_filename
=
"
${
z
}
.pdf"
pdf_path
=
"./
${
pdf_dir
}
/
${
pdf_filename
}
"
}
construct_bak_pdf_path
()
{
bak_pdf_filename
=
"
${
z
}
_
${
file_date
}
-
${
file_time
}
.pdf"
# create new filename
bak_pdf_path
=
"./
${
pdf_dir
}
/
${
bak_pdf_filename
}
"
}
# END functions #
pdf_dir
=
"PDFs"
if
[
-f
$pdf_path
]
;
then
echo
"File
$i
was already converted."
replace_whitespaces
create_filearray
find_markdown
create_outdir_if_not_exists
select_latex_engine
# # Allow user to add custom LaTeX variables
# echo -n "Would you like to add any LaTeX details(e.g. --variable mainfont:\"Gill Sans\")?: "
# read variable
# Compile documents
echo
-e
"
\n
Beginning conversion...
\n
"
# find . -iname "*.md" -o -iname "*.mdown" -o -iname "*.markdown" | while read -r file; do # find -iname finds <pattern> case insensitive
for
file
in
"
${
files
[@]
}
"
;
do
echo
"File:"
${
file
}
extract_filename
construct_pdf_path
construct_bak_pdf_path
echo
"PDF Path:
$pdf_path
"
if
[
-f
"
$pdf_path
"
]
;
then
echo
"File
$file
was already converted."
create_date_string
echo
"Bak file name:
$bak_pdf_filename
"
mv
$pdf_path
$bak_pdf_path
echo
"
Existing file was
renam
ed to
$bak_pdf_
filename
"
echo
-e
"
\n
Existing file was
mov
ed to
$
{
bak_pdf_
path
}
\n
"
fi
pandoc
-f
markdown
$i
--pdf-engine
=
$option
--variable
font-family:sans-serif
$variable
-o
$pdf_path
# ((count++))
# echo "$count file compiled."
echo
-e
"Continuing with new file creation
\n
"
pandoc
-s
--toc
--template
=
my.latex
-f
markdown
"
$file
"
--pdf-engine
=
$option
-o
"
$pdf_path
"
# could add --variable font-family:sans-serif $variable
echo
-e
"File created
\n
"
done
echo
-e
"
\n
All files complete!"
\ No newline at end of file
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment