generate_page.sh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. #!/bin/sh
  2. ad="
  3. <iframe src=\"https://viewm.moonicorn.network/#%7B%22options%22%3A%7B%22publisherAddr%22%3A%220x4C191717BA8F3b5Ee19e5B4F6975023529E5984C%22%2C%22whitelistedTokens%22%3A%5B%220x6B175474E89094C44Da98b954EedeAC495271d0F%22%5D%2C%22whitelistedType%22%3A%22legacy_300x250%22%2C%22randomize%22%3Atrue%2C%22targeting%22%3A%5B%5D%2C%22width%22%3A%22300%22%2C%22height%22%3A%22250%22%2C%22minPerImpression%22%3A%220%22%2C%22fallbackUnit%22%3A%22QmX2SLnCVEB1c8kY41tM4cLPtXi3h5XFMuNi4Lt1bfvjyf%22%2C%22marketSlot%22%3A%22QmNN1UY4iNekLWjDVodtvZfSKgWCnf9UU7PXsDQRHDEAdG%22%7D%7D\"
  4. width=\"300\"
  5. height=\"250\"
  6. scrolling=\"no\"
  7. frameborder=\"0\"
  8. style=\"border: 0;\"
  9. onload=\"window.addEventListener('message', function(ev) {
  10. if (ev.data.hasOwnProperty('adexHeight') && ('https://viewm.moonicorn.network' === ev.origin)) {
  11. for (let f of document.getElementsByTagName('iframe')) {
  12. if (f.contentWindow === ev.source) {
  13. f.height = ev.data.adexHeight;
  14. }
  15. }
  16. }
  17. }, false)\"
  18. ></iframe>"
  19. #
  20. # source is the json file that describes a page
  21. # destination is the final html file
  22. #
  23. target_src=$2
  24. target_dst=$1
  25. #
  26. # Parse data for destination file
  27. #
  28. title=$(jq -r .title $target_src)
  29. description=$(jq -r .description $target_src)
  30. contentType=$(jq -r .contentType $target_src)
  31. #
  32. # get root location based on the filename
  33. #
  34. root="."
  35. IFS='/'
  36. read -a parentDir <<< "$target_dst"
  37. unset IFS
  38. i=2
  39. while [[ $i -lt ${#parentDir[@]} ]]; do
  40. root=$root"\/.."
  41. i=$(( $i + 1 ))
  42. done
  43. #
  44. # template for all pages
  45. #
  46. TEMPLATE=$(cat md_files/template.md)
  47. #
  48. # static content files
  49. #
  50. static_content_files=()
  51. static_content_file=$(jq -r .content_static $target_src)
  52. if [[ "$static_content_file" != "null" ]]; then
  53. static_content_files+=($static_content_file)
  54. fi
  55. #
  56. # collect all content files in an array
  57. #
  58. content_files=()
  59. content_file=$(jq -r .content[0] $target_src)
  60. i=1
  61. while [[ "$content_file" != "null" ]]; do
  62. content_files+=($content_file)
  63. content_file=$(jq -r .content[$i] $target_src)
  64. i=$(($i +1))
  65. done
  66. #
  67. # reverse order so newer ones come first
  68. #
  69. IFS=$'\n' content_files=($(sort -r <<<"${content_files[*]}"))
  70. unset IFS
  71. #
  72. # print pages
  73. #
  74. i=0
  75. pageCount=0
  76. while [[ $i -lt ${#content_files[@]} ]]; do
  77. hasShownAd=0
  78. content=""
  79. if [[ $contentType == "grid" ]]
  80. then
  81. # parent div
  82. content=$(echo -e "$content <div class=\"content-grid\">")
  83. fi
  84. #
  85. # print all static content to a variable
  86. #
  87. for j in "${static_content_files[@]}"; do
  88. content=$(echo -e "$content $(cat $j | sed s/0/$pageCount/ | markdown)<hr style='display:none;'>")
  89. done
  90. #
  91. # print all content to a variable, 5 posts per page
  92. #
  93. j=0
  94. while [[ $i -lt ${#content_files[@]} ]] && [[ $j -lt 5 ]]; do
  95. # add an ad before the third post on a page with multiple posts
  96. if [[ $j -eq 2 && $hasShownAd == 0 ]]; then
  97. hasShownAd=1
  98. content=$(echo -e "$content$ad")
  99. fi
  100. #
  101. # get filename, to parse date
  102. #
  103. filename=""
  104. IFS='/'
  105. read -a strarr <<< "${content_files[$i]}"
  106. unset IFS
  107. filename=${strarr[-1]}
  108. #
  109. # parse date - if file contains it
  110. #
  111. if [[ "$filename" =~ ^[0-9]{12} ]]
  112. then
  113. date="${filename:0:4}${filename:4:2}${filename:6:2} ${filename:8:2}:${filename:10:2}"
  114. date="<p class=\"center-aligned\">"$(date --date="$date" +"%Y.%m.%d - %a %H:%M")"</p>"
  115. else
  116. date=""
  117. fi
  118. if [[ $i -ne 0 ]]; then
  119. content=$(echo -e "$content<hr class='hidden'>")
  120. fi
  121. #
  122. # apply contents
  123. #
  124. # grid is showing small tiles, 3 per row
  125. # input is json data
  126. #
  127. if [[ $contentType == "grid" ]]
  128. then
  129. #content=$(echo -e "$content <div class=\"content-grid\">$(cat ${content_files[$i]} | markdown) $date</div>")
  130. gameName=$(jq -r .name ${content_files[$i]})
  131. #gameDescription=$(echo -e $(jq -r .description ${content_files[$i]} | markdown))
  132. gameDescriptionShort=$(echo -e $(jq -r .description_short ${content_files[$i]}))
  133. gameCover=$(jq -r .cover ${content_files[$i]})
  134. gamePrice=$(jq -r .price ${content_files[$i]})
  135. # prepare tile
  136. content=$(echo -e "$content <div class=\"content-grid-tile\">")
  137. # name and description
  138. #content=$(echo -e "$content $gameDescription")
  139. content=$(echo -e "$content <img src=\"..$gameCover\">")
  140. content=$(echo -e "$content <h4>$gameName</h4>\n")
  141. content=$(echo -e "$content <p class=\"content-grid-tile-description\">$gameDescriptionShort</p>")
  142. content=$(echo -e "$content <p class=\"content-grid-tile-price\">$gamePrice</p>")
  143. # # links
  144. # gameLinkName=$(jq -r .links[0].name ${content_files[$i]})
  145. # gameLinkTarget=$(jq -r .links[0].link ${content_files[$i]})
  146. # linkNum=1
  147. # while [[ "$gameLinkName" != "null" ]]; do
  148. # content=$(echo -e "$content $gameLinkName")
  149. # content=$(echo -e "$content $gameLinkTarget")
  150. # gameLinkName=$(jq -r .links[$linkNum].name ${content_files[$i]})
  151. # gameLinkTarget=$(jq -r .links[$linkNum].link ${content_files[$i]})
  152. # linkNum=$(($linkNum +1))
  153. # done
  154. #
  155. # # screenshots
  156. # gameScreenshot=$(jq -r .screenshots[0] ${content_files[$i]})
  157. # screenshotNum=1
  158. # while [[ "$gameScreenshot" != "null" ]]; do
  159. # content=$(echo -e "$content $gameScreenshot")
  160. # gameScreenshot=$(jq -r .screenshots[$screenshotNum] ${content_files[$i]})
  161. # screenshotNum=$(($screenshotNum +1))
  162. # done
  163. # end tile
  164. content=$(echo -e "$content $date</div>")
  165. # normal markdown content
  166. else
  167. localContent=$(cat ${content_files[$i]})
  168. if [[ "$localContent" == *"@DD-AD@"* && $hasShownAd == 0 ]]; then
  169. echo "show ad"
  170. hasShownAd=1
  171. localContent=$(echo -e "${localContent/@DD-AD@/$ad}")
  172. fi
  173. localContent=$(echo -e "${localContent//@DD-AD@/}")
  174. if [[ $contentType != "html" ]]; then
  175. localContent=$(echo -e "${localContent}" | markdown)
  176. fi
  177. content=$(echo -e "$content <div class=\"content\">$localContent $date</div>")
  178. fi
  179. i=$(( $i + 1))
  180. j=$(( $j + 1 ))
  181. done
  182. if [[ $contentType == "grid" ]]
  183. then
  184. # parent div close
  185. content=$(echo -e "$content </div>")
  186. fi
  187. #
  188. # for pages after the first, add `-$i` to its name
  189. #
  190. IFS='.'
  191. read -a strarr <<< "$target_dst"
  192. unset IFS
  193. filename=${strarr[-2]}
  194. if [[ $pageCount -gt 0 ]]; then
  195. filename=$filename-$pageCount.${strarr[-1]}
  196. else
  197. filename=$target_dst
  198. fi
  199. #
  200. # page buttons
  201. #
  202. pageIndex=$(( $pageCount + 1 ))
  203. #
  204. # Not first page, so prepare "previous" functionality
  205. #
  206. if [ "$pageCount" -gt 0 ]; then
  207. # check previous page name
  208. if [ "$pageCount" -eq 1 ]; then
  209. if [[ "$target_dst" == "_site/index.html" ]]; then
  210. PAGE="."
  211. else
  212. PAGE=$target_dst
  213. fi
  214. else
  215. PAGE=${strarr[-2]}-$(( $pageCount -1 )).${strarr[-1]}
  216. fi
  217. IFS='/'
  218. read -a filearr <<< "$PAGE"
  219. unset IFS
  220. previousButton="<a class=\"menu\" href='${filearr[-1]}'><b><</b></a>"
  221. else
  222. previousButton=""
  223. fi
  224. #
  225. # Next page button
  226. #
  227. if [ ! -z "${content_files[i]}" ]; then
  228. PAGE=${strarr[-2]}-$(( $pageCount +1 )).${strarr[-1]}
  229. IFS='/'
  230. read -a filearr <<< "$PAGE"
  231. unset IFS
  232. nextButton="<a class=\"menu\" href='${filearr[-1]}'><b>></b></a>"
  233. else
  234. nextButton=""
  235. fi
  236. #
  237. # the final text: <previousButton> 0 1 2 .. N <nextButton>
  238. #
  239. pageButton="<p class=\"center-aligned\">$previousButton "
  240. for (( j=0; j<=$(( (${#content_files[@]}-1) / 5 )); j++))
  241. do
  242. if [ $j -eq 0 ]
  243. then
  244. PAGE=${strarr[-2]}.${strarr[-1]}
  245. else
  246. PAGE=${strarr[-2]}-$j.${strarr[-1]}
  247. fi
  248. IFS='/'
  249. read -a filearr <<< "$PAGE"
  250. unset IFS
  251. if [ $j -eq 0 ]
  252. then
  253. if [ "$PAGE" == "_site/index.html" ]
  254. then
  255. destination="./"
  256. else
  257. destination=${filearr[-1]}
  258. fi
  259. else
  260. destination=${filearr[-1]}
  261. fi
  262. if [ $pageCount -eq $j ]
  263. then
  264. pageButton=$pageButton" <a class=\"menu-selected\" href='$destination'><b>"$j"</b></a>"
  265. else
  266. pageButton=$pageButton" <a class=\"menu\" href='$destination'><b>"$j"</b></a>"
  267. fi
  268. done
  269. pageButton="<hr style='display:none'>$pageButton $nextButton</p>"
  270. if [ ${#content_files[@]} -le 5 ]
  271. then
  272. pageButton=""
  273. fi
  274. #
  275. # add buttons on each page
  276. #
  277. content=$(echo -e "$content $pageButton")
  278. #
  279. # prepare page specific details
  280. #
  281. if [ "$pageCount" -gt 0 ]; then
  282. pageTitle="$title #$pageCount"
  283. else
  284. pageTitle=$title
  285. fi
  286. #
  287. # print variable to destination file
  288. #
  289. echo -e "${TEMPLATE/@CONTENT@/${content}}" | sed "s/@STYLE_PREFIX@//g" | sed "s/@ROOT@/$root/g" |\
  290. sed "s/@TITLE@/$pageTitle/g" | sed "s/@DESCRIPTION@/$description/g" > $filename
  291. pageCount=$(( $pageCount + 1 ))
  292. done