1 #!/bin/bash
  2 # make HTML posts from sources
  3 # concatenate posts
  4 # v0.2.12  sep/2020  mountaineerbr
  5 
  6 #directory structure
  7 #blog post directories start with a number /[0-9]* .
  8 #the post directory must hold an i.html file, which
  9 #contains aditional metatags within <head> tags and
 10 #the post article itself within <article> tags.
 11 #ex: blogRootDir/1/i.html
 12 
 13 #i.html files are processed/compiled to generate
 14 #index.html files at the same directory of i.html files.
 15 #all articles are then concatenated to cat.html at
 16 #the blog root directory.
 17 #previously generated files are replaced with new ones.
 18 
 19 #source src paths must be fixed!
 20 
 21 
 22 #defaults
 23 
 24 #home page root
 25 ROOT="$HOME/www/mountaineerbr.github.io/"
 26 #blog root
 27 ROOTB="$ROOT/blog"
 28 
 29 #template files
 30 TEMPLATE_POST="$ROOTB/p.html"
 31 TEMPLATE_CAT="$ROOTB/c.html"
 32 
 33 #set targets
 34 #post titles
 35 TARGET_TITLES="$ROOTB/titles.txt"
 36 TARGET_TITLES_HOME="$ROOTB/titles.homepage.txt"
 37 #all posts 
 38 TARGET_CAT="$ROOTB/cat.html"
 39 #blog index
 40 TARGET_BLOGIND="$ROOTB/index.html"
 41 
 42 #target home page index.html
 43 TARGET_HOME="$ROOT/index.html"
 44 
 45 #PART ZERO
 46 #start
 47 #exit on any error
 48 set -e
 49 
 50 #test target files exist
 51 #changing to $ROOTB is required!
 52 cd "$ROOTB"
 53 
 54 #check for template files
 55 for t in "$TEMPLATE_CAT" "$TEMPLATE_POST"
 56 do
 57         if [[ ! -f "$t" ]]
 58         then
 59                 echo "template file missing -- $t" >&2
 60                 exit 1
 61         fi
 62 done
 63 unset t
 64 
 65 
 66 #feedback
 67 echo 'generate an array with raw post paths..' >&2
 68 #make an array with raw post filepaths
 69 #raw post = i.html
 70 while IFS=  read
 71 do
 72         POSTFILE+=( "$REPLY" )
 73 done <<< "$( printf '%s\n' */i.html | sort -nr )"
 74 unset REPLY
 75 
 76 #check directory array is not empty
 77 (( ${#POSTFILE[@]} )) || exit 1
 78 
 79 #make sure TARGET_TITLES exist (avoids grep errors)
 80 [[ ! -f "$TARGET_TITLES" ]] && : > "$TARGET_TITLES"
 81 
 82 #feedback
 83 echo 'compile index.html files for individual posts..' >&2
 84 n="${#POSTFILE[@]}"
 85 for f in "${POSTFILE[@]}"
 86 do
 87         #PART ONE
 88         #make post html pages from templates
 89         #set post index.html path
 90         targetpost="${f/i.html/index.html}"
 91 
 92         #partial/buffer file
 93         TARGETPOST_TEMP="${targetpost}.part"
 94 
 95         #add title and meta tags to buffer file
 96         sed -n '/<head>/,/<\/head>/ p' "$f" |
 97                 sed 's/^<\/*head>// ; /^\s*$/d' |
 98                 sed '/^<!-- metatags -->/ r /dev/stdin' "$TEMPLATE_POST" > "$TARGETPOST_TEMP"
 99 
100         #add article and create final html page
101         sed -n '/<article.*>/,/<\/article>/ p' "$f" |
102                 sed '/^<!-- article -->/ r /dev/stdin' "$TARGETPOST_TEMP" > "$targetpost"
103         #https://stackoverflow.com/questions/46423572/append-a-file-in-the-middle-of-another-file-in-bash
104 
105 
106         #PART TWO
107         #make a post title list
108         #the resulting file can be used by other scripts
109         TAGPX='<!-- postlistX -->'
110         TARGET_TITLES_TEMP="${TARGET_TITLES}.new"
111         {
112                 #add tags to titles.txt.new
113                 ((n==${#POSTFILE[@]})) && echo "$TAGPX"
114 
115                 #make the post title list item
116                 #try to grep for a similar post title with additional properties (like hreflang)
117                 #in html string in "old" $TARGET_TITLES
118                 if tili="$( sed -nE 's|(.*<title>)([^"]+)<.*|\2| p' "$f" | sed 's|\s--\sBiol.*||' )"
119                         ! grep -Fi "$tili" "$TARGET_TITLES"
120                 then
121                         #print a default <li> item if none found on "old" $TARGET_TITLES file
122                         <<< "$tili" sed "s|^|<li><a href=\"${f%\/*}\/\">|g"  |
123                         sed 's|$|</a></li>|g'
124                 fi
125 
126                 #add tags to titles.txt.new
127                 ((n==1)) && echo "$TAGPX"
128 
129         } >> "$TARGET_TITLES_TEMP"
130 
131         #counter
132         ((--n))
133 
134         #keep environment clean
135         unset f TARGETPOST_TEMP targetpost s ss sss tili tile
136 
137 done || true  #don't get caught by `set -e` here
138 
139 #move (rename) new titles file
140 mv -fv "$TARGET_TITLES_TEMP" "$TARGET_TITLES"
141 
142 #remove buffer file
143 rm -v -- */*.part
144 
145 
146 #PART THREE
147 #feedback
148 echo 'compile cat.html with all posts..' >&2
149 #reuse array $POSTFILE file list
150 #that list is in reverse order
151 #concatenate posts
152 #make new cat.html
153 sed -n '/<article.*>/,/<\/article>/ p' "${POSTFILE[@]}" |
154         sed '/^<!-- articles -->/ r /dev/stdin' "$TEMPLATE_CAT" > "$TARGET_CAT"
155 
156 #add breaks between articles
157 #obs: this can set in CSS stylesheet as padding!
158 sed -i 's/<article.*>/<br><br>\n&/' "$TARGET_CAT"
159 
160 #change src relative paths of cat.html
161 #make changes in-place in cat.html
162 #note there are exceptions..
163 #also fix empty href=""
164 s='(href|src)='
165 ss='="\.\.\/'
166 sss='="'
167 sed -Ei -e "/${s}/ { /favicon.ico/! s|${ss}|${sss}|g }" \
168         -e 's|href=""|href="./"|g' \
169         "$TARGET_CAT"
170 #https://unix.stackexchange.com/questions/502230/how-to-apply-sed-if-it-contains-one-string-but-not-another/502233
171 #try to avoid 'picket fence' in sed commands!
172 #http://johnbokma.com/blog/2016/08/02/tar-files-in-other-directory.html  
173 
174 
175 #PART FOUR
176 #remove old list from BLOG home page
177 #add post titles to blog/index.html
178 sed -i "/$TAGPX/,/$TAGPX/ d" "$TARGET_BLOGIND"
179 sed -i "/^\s*<!-- postlist -->/ r $TARGET_TITLES" "$TARGET_BLOGIND"
180 
181 
182 #PART FIVE
183 #make a post title file for WEBSITE home page
184 #must fix title list href!
185 #get only the top 10 post titles
186 sed 's|href="|href="blog/|' "$TARGET_TITLES" |
187         sed '11,$ s|<li>.*|| ; /^$/ d' > "$TARGET_TITLES_HOME"
188 
189 #update WEBSITE home page with latest post list
190 sed -i "/$TAGPX/,/$TAGPX/ d" "$TARGET_HOME"
191 sed -i "/^\s*<!-- postlist -->/ r $TARGET_TITLES_HOME" "$TARGET_HOME"
192 
193 
194 #PART SIX
195 #transient tasks
196 #
197 #
198 
199 
200 
201 #REFS
202 
203 #TODO
204