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
a357c98a
Commit
a357c98a
authored
Nov 26, 2021
by
Sabina Köfler
Browse files
Create shell script to convert .md to .pdf
parent
9f47c3eb
Changes
1
Show whitespace changes
Inline
Side-by-side
quick-start-guides/convert_to_pdf.sh
0 → 100755
View file @
a357c98a
#!/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
echo
-e
"No markdown files detected!
\n
"
exit
fi
pdf_dir
=
"PDFs"
# Checks for output directory and creates one if missing
if
[
!
-d
$pdf_dir
]
;
then
mkdir
$pdf_dir
echo
-e
"Output folder created.
\n
"
else
echo
-e
"Selecting existing output folder.
\n
"
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
case
$option
in
pdflatex
)
echo
-e
"
\n
pdflatex engine selected"
;;
lualatex
)
echo
-e
"
\n
lualatex engine selected"
;;
xelatex
)
echo
-e
"
\n
xelatex"
;;
*
)
echo
"Please choose an option"
;;
esac
break
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
# 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
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
bak_pdf_filename
=
"
${
z
}
_
${
file_date
}
-
${
file_time
}
.pdf"
# create new filename
bak_pdf_path
=
"./
${
pdf_dir
}
/
${
bak_pdf_filename
}
"
if
[
-f
$pdf_path
]
;
then
echo
"File
$i
was already converted."
mv
$pdf_path
$bak_pdf_path
echo
"Existing file was renamed to
$bak_pdf_filename
"
fi
pandoc
-f
markdown
$i
--pdf-engine
=
$option
--variable
font-family:sans-serif
$variable
-o
$pdf_path
# ((count++))
# echo "$count file compiled."
done
echo
-e
"
\n
All files complete!"
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