-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlinearize
executable file
·150 lines (136 loc) · 4.07 KB
/
linearize
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
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2021-2022 Robin Vobruba <[email protected]>
#
# SPDX-License-Identifier: GPL-3.0-or-later
# Builds a single Markdown file out of an index of Markdown files.
# We do that by using Pandoc with custom filters.
#
# This is part of the [MoVeDo](https://github.com/movedo) project.
# See the LICENSES/ folder for copyright information.
script_dir=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
# shellcheck source=./_common.sh
. "$script_dir/_common.sh"
tools=()
tools+=("pandoc") # for Markdown filtering/cleanup
tools+=("python3") # for Panflute
tools+=("panflute") # for Pandoc filters
_check_tool "$script_dir/strip_front_matter"
_check_tools "${tools[@]}"
_check_env
# parameters
cd "$gen_src_dir"
rm -f "$single_md"
pandoc_extra=()
pandoc_extra+=("--citeproc")
function global_doc_meta() {
if [ -f "$doc_meta_file" ]
then
doc_meta_file_gen="$gen_src_dir/$(basename "$doc_meta_file")"
# -e '/^#.*$/d' \
sed \
-i \
-e '/^[-]\{3,\}$/d' \
-e '/^[.]\{3,\}$/d' \
"$doc_meta_file_gen"
echo "---"
echo "# HACK The first line after the above '---' needs to be a comment,"
echo "# or else this meta-data does not end up in the generated PDF,"
echo "# ... wierd!"
cat "$doc_meta_file_gen"
echo "---"
echo
fi
}
echo
echo "Generating single, linear Markdown file ..."
# This meta-data file is used for each markdown file to be converted,
# but only during conversion.
# We need this, as it is not possible to supply a YAML array
# with "-M" to pandoc, which we require for "panflute-filters".
common_lin_meta_file="$(mktemp --suffix=".yml")"
# Remove this file when script exits
trap 'rm -rf -- "$common_lin_meta_file"' EXIT
{
echo "panflute-path: \"$filters_dir\""
echo "panflute-filters: [add_local_link_prefix, normalize_links, linearize_links]"
# Uncomment this next line for debugging
# echo "panflute-verbose: true"
} > "$common_lin_meta_file"
if [ "$(wc -l < "$index_md_file")" -eq 1 ]
then
# Build from a single source file
md_in="$(cat "$index_md_file")"
single_md_tmp="${single_md}_tmp"
rm -f "$single_md_tmp"
# Use global document meta-data
global_doc_meta >> "$single_md_tmp"
# Use the single source file as-is
cat "$md_in" >> "$single_md_tmp"
echo " adding '$md_in' ..."
pandoc \
--from=markdown \
--to=markdown \
--standalone \
--markdown-headings=atx \
-M allp_prefix="$(dirname "$md_in")/" \
-M allp_file="$md_in" \
-M ll_doc_path="" \
--metadata-file="$common_lin_meta_file" \
-F panflute \
"${pandoc_extra[@]}" \
< "$single_md_tmp" \
>> "$single_md"
rm "$single_md_tmp"
elif [ -f "$doc_meta_file" ]
then
# Build from multiple source files
# Use global document meta-data
global_doc_meta >> "$single_md"
while read -d $'\n' -r md_in
do
echo "Linearizing $md_in ..."
if [ ! -e "$md_in" ]
then
_error "File '$md_in' not found! It is specified in the index file '$index_md_file'."
fi
# TODO put this in its own script:
# extract the document(-fragment)s title from the YAML meta-data
grep "^title: " "$md_in" > /dev/null && {
title=$(grep "^title: " "$md_in" \
| head -n 1 \
| sed -e 's/^title:[[:space:]]\+//')
echo -e "# $title\n" >> "$single_md"
}
# here we do:
# * strip front-matter
# * cleanup
# * filter
# * add_local_link_prefix
# * normalize_links
# * linearize_links
# * write to the global document file
echo " adding '$md_in' ..."
"$script_dir/strip_front_matter" "$md_in" \
| pandoc \
--from=markdown \
--to=markdown \
--markdown-headings=atx \
-M allp_prefix="$(dirname "$md_in")/" \
-M allp_file="$md_in" \
-M ll_doc_path="$md_in" \
--metadata-file="$common_lin_meta_file" \
-F panflute \
"${pandoc_extra[@]}" \
>> "$single_md"
echo -e "\n" >> "$single_md"
done < "$index_md_file"
else
if [ "${MVD_SINGLE_FILE:-true}" = "true" ]
then
_error "If you have more then one source file, you need '$doc_meta_file', \
containing the YAML Front-Matter for the combined document, \
or you need to set the env var MVD_SINGLE_FILE to 'false'.
Your index:
$(cat "$index_md_file")"
fi
fi