-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.sh
executable file
·1619 lines (1349 loc) · 49 KB
/
main.sh
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env sh
# usage function
usage() {
echo "sh main.sh config ------> generate an easy to edit config file"
echo "sh main.sh init --------> initialize all files based on sitemap section in config.txt"
echo "sh main.sh html filename.md -------> generate html format for the filename.md"
echo "sh main.sh navgen ------> generate navigation section from config.txt sitemap section and push it in navigation section of config_file"
echo "sh main.sh post -------> make a post"
echo "sh main.sh add ---------> add a post and also an entry to a base.md file and also config.txt sitemap section"
echo "sh main.sh adddir ---------> add a whole directory navigation page to all files"
echo "sh main.sh rmdir -----------> remove a directory navigation entry page from all files"
echo "sh main.sh remove latest -------> will remove the latest entry made through running sh main.sh add"
echo "sh main.sh remove last dirname/base.md --------> will remove the last article entry from dirname/base.md file (it has to be a base.md file)"
echo "sh main.sh all -------> convert all md files(mentioned in config_file) to html files"
echo "sh main.sh index index.md -------> convert index.md file to index.html"
echo "sh main.sh final -------> arrange all files to a main or final site directory"
echo "sh main.sh rss ----------> generate a rss feed of the articles from base.md files"
}
# global variables
# config file name
config_file="config.txt"
# index file name
index_file="index.md"
# main site dir variable name
main_site="main-site"
# css and js dirs
css_dir="css"
js_dir="js"
# rss file name
rss_file="rss.xml"
# assets directory
assets_dir="assets"
## global functions
## check if config file exists
check_config() {
[ ! -f "$config_file" ] && \
echo "file $config_file not found, exiting..." && \
exit 1;
}
## check if sitemap section is mentioned in config file
check_sitemap() {
grep -q "^++.*sitemap$" $config_file && grep -q "^--.*sitemap$" $config_file
if [ "$?" -ne 0 ]; then
echo "$config_file does not have a sitemap region mentioned, exiting..." && \
exit 1;
fi
# if sitemap region is less than 3 lines then abort
if [ "$(sed -n '/^++.*sitemap$/,/^--.*sitemap$/p' $config_file | wc -l)" -le 3 ]; then
echo "sitemap region is improperly edited, exiting..." && \
exit 1;
fi
}
## check if navigation section is mentioned in config file
nav_check() {
# check for navigation region in config file
grep -q "^+.*navigation$" $config_file && grep -q "^-.*navigation$" $config_file
if [ "$?" -ne 0 ]; then
echo "navigation section is not mentioned in $config_file, perhaps you would want to edit it manually exiting..." && \
exit 1;
fi
}
## value variable without index file
vals_noindex() {
# getting values from sitemap region
vals="$(sed -n '/^+.*sitemap$/,/^-.*sitemap$/p' $config_file | \
sed '/^\(+\|-\)/d' | grep ".md$" | grep -v "$index_file")"
}
## value variable with basemd file
vals_basemd() {
# grep base.md values only
vals="$(sed -n '/^+.*sitemap$/,/^-.*sitemap$/p' $config_file | \
sed '/^\(+\|-\)/d' | grep "base.md$")"
}
## value variable with all md5 files
vals_allmd() {
# grep base.md values only
vals="$(sed -n '/^+.*sitemap$/,/^-.*sitemap$/p' $config_file | \
sed '/^\(+\|-\)/d' | grep ".md$")"
}
## value variable with all files in sitemap section
vals_all() {
# grep base.md values only
vals="$(sed -n '/^+.*sitemap$/,/^-.*sitemap$/p' $config_file | \
sed '/^\(+\|-\)/d')"
}
## extra post section for quick access, rather than writing same function repeatedly
## asks for title
ask_title() {
read -p "Enter the title of the page: " title
val="$title" && empty_check
}
## asks for description about the article
ask_describe() {
read -p "Enter little bit of description about the page[optional]: " describe
arg="$author" && skip
}
## asks for author name
ask_author() {
read -p "Enter the author of the page[optional]: " author
arg="$author" && skip
}
## ask for name of file
ask_name() {
read -p "Name of the file you're about to edit: " current
val="$current" && empty_check
}
## generate template for config file
config_generate() {
# create config file
[ ! -f "$config_file" ] && \
touch $config_file
grep -q "^++.*sitemap" $config_file && grep -q "^--.*sitemap$" $config_file
if [ "$?" -ne 0 ]; then
echo "# Sitemap section -> include files.md here" >> $config_file
echo "" >> $config_file
echo "++++++++++sitemap\n--------sitemap" >> $config_file
fi
grep -q "^++.*navigation" $config_file && grep -q "^--.*navigation$" $config_file
if [ "$?" -ne 0 ]; then
echo "" >> $config_file
echo "++++navigation\n.homepage: [home](index.html)\n.navmenu: roam\n.backpage: [base](base.html)" >> $config_file
echo "----------navigation" >> $config_file
fi
grep -q "^++.*sitelink" $config_file && grep -q "^--.*sitelink" $config_file
if [ "$?" -ne 0 ]; then
echo "" >> $config_file
echo "# This portion is necessary for rss.xml generation. Rss portion starts from here\n" >> $config_file
echo "+++++sitelink\nhttps://yoursitename.com\n------sitelink" >> $config_file
echo "" >> $config_file
echo "+++++description\nsome description about your site\n-----description" >> $config_file
echo "" >> $config_file
echo "++++title\nthe title of your site\n-----title" >> $config_file
echo "" >> $config_file
echo "# Rss generation portion ends here. The Rss portion is optional" >> $config_file
fi
grep -q "^# toggle script" $config_file
if [ "$?" -ne 0 ]; then
echo "\n# toggle script\n# this portion below is necessary\n" >> $config_file
echo "+++++++script\nmode = document.getElementById('switch');\n" >> $config_file
echo "if (! navigator.cookieEnabled) {\n\tmode.style.display = 'none';\n}" >> $config_file
echo "else if(! localStorage) {\n\tmode.style.display = 'none';\n}" >> $config_file
echo "else {\n\tmode.style.display = 'inline';\n}\n------script\n" >> $config_file
echo "# you can also add custom script files like this\n" >> $config_file
echo "+++++++++add\n.script: js/toggle.js\n-----------add" >> $config_file
echo "# this portion above in between ++add and --add is necessary\n" >> $config_file
echo "# Footer section (the message portion can be changed)\n" >> $config_file
echo "++++++++++++++++footer\n.class: footer\n.message: Made with <3 by [samiuljoy](https://github.com/samiuljoy)" >> $config_file
echo "------------------footer" >> $config_file
fi
}
## initiate everything
init() {
# check for config file
check_config
# check for sitemap region
check_sitemap
# get values from sitemap region
vals_noindex
# creating index.md file
if [ ! -f "index.md" ]; then
touch index.md
else
echo "index.md file exists, skipping creating a new one"
fi
# make css, js and assets dir
[ ! -d "$css_dir" ] && \
mkdir -p "$css_dir"
[ ! -d "$js_dir" ] && \
mkdir -p "$js_dir"
[ ! -d "$assets_dir" ] && \
mkdir -p "$assets_dir"
# initialize directories and files based on $sitemap
for i in $vals; do
if [ -d "$(dirname $i)" ]; then
touch "$i"
echo "created $i"
else
mkdir -p "$(dirname $i)" && \
echo "made dir $(dirname $i)" && \
touch "$i" && \
echo "created $i"
fi
done
# exit status check
[ "$?" = 0 ] && \
echo "directories and files initialized" || \
echo "something went wrong" && \
return 1;
}
## navigation generation
navigation_gen() {
# check for config file
check_config
# check for sitemap region
check_sitemap
# grep basemd values only
vals_basemd
# check for navigation intent
nav_check
line_count="$(sed -n '/^+.*navigation$/,/^-.*navigation$/p' $config_file | wc -l)"
if [ "$line_count" -ge "6" ]; then
read -p "Seems like you made some changes to the navigation section of $config_file, are you sure you want to make further changes? [y/n]: " write
case "$write" in
y|Y|yes|Yes ) break
;;
n|N|no|No ) echo "not making any changes to the navigation section, exiting..." && \
return 1
;;
* ) echo "Invalid value..exiting, making no changes.." && \
return 1
;;
esac
fi
# if everything above returns true, then run this section
# generate navigation values based on dir name
for i in $(echo $vals); do
ddd="$(dirname $i)"
html_val="$(echo $i | sed 's/.*\/\(.*\).md/\1.html/g')"
ddd_val="$(dirname $i | sed 's/\//\\\//g')"
grep -q "\.navpage: \[$ddd_val\]($ddd_val/$html_val)" $config_file
if [ "$?" -ne 0 ]; then
sed -i "/^\.backpage:\s/i .navpage: [$ddd_val]($ddd_val\/$html_val)" $config_file
fi
done
}
## transform markdown article to html
main_generate() {
# needs serious refactoring
# null argument
[ -z "$1" ] && \
echo "empty file passed" && \
return 1;
# check if file has a html extension
echo $1 | grep -q "\.html" && \
echo "file with .html can not be modified, use .md(markdown)" && \
return 1;
# check if file exists
[ ! -f "$1" ] && echo "file $1 does not exist" && return 1;
# check if file is empty, if empty then skip file
[ ! -s "$1" ] && \
echo "file seems to be empty, skipping $1" && \
return 1;
# functions
file_rename() {
filename="$(echo $orig | sed 's/\(.*\).md/\1.html/g')"
touch $filename && cat $orig > $filename
}
# function end
# args
if [ "$1" = "-c" ]; then
orig="$1"
# calling file_rename function
file_rename
else
orig="$1"
file_rename
fi
# loop through code blocks, and substitute code blocks to new file/s
grep -q "^\`\`\`[[:digit:]]" $filename
if [ "$?" = 0 ]; then
code_number=1
upto="$(grep '^```[[:digit:]]\+$' $filename | tail -n1 | cut -c 4-)"
code_directory="$(dirname $filename)/code"
mkdir -p "$code_directory"
while [ "$code_number" -le "$upto" ]; do
sed -n "/^\`\`\`$code_number$/,/^\.code$code_number$/p" $filename > "$filename-code$code_number.txt"
sed -i '/^\.code[[:digit:]]\+/d' "$filename-code$code_number.txt"
sed -i '/^```/d' "$filename-code$code_number.txt"
sed -i 's/^\t//g' "$filename-code$code_number.txt"
mv "$filename-code$code_number.txt" $code_directory
code_number="$(( $code_number + 1 ))"
done
fi
# escape sequences substitution -> bounded
sed -i '/^```.*$/,/^```$/ s/\./\./g' $filename
sed -i '/^```.*$/,/^```$/ s/_/\_/g' $filename
sed -i '/^```.*$/,/^```$/ s/\!/\!/g' $filename
sed -i '/^```.*$/,/^```$/ s/\[/\[/g' $filename
sed -i '/^```.*$/,/^```$/ s/\]/\]/g' $filename
sed -i '/^```.*$/,/^```$/ s/~/\∼/g' $filename
sed -i '/^```.*$/,/^```$/ s/\*/\*/g' $filename
sed -i '/^```.*$/,/^```$/ s/#/\#/g' $filename
sed -i '/^```.*$/,/^```$/ s/</\</g' $filename
sed -i '/^```.*$/,/^```$/ s/>/\>/g' $filename
# global escape sequences substitution
sed -i 's/\\_/\_/g' $filename
sed -i 's/\\\*/\*/g' $filename
sed -i 's/\\`/\`/g' $filename
sed -i 's/\\\[/\[/g' $filename
sed -i 's/\\\]/\]/g' $filename
# global substitution < and > to escape sequences < and >
sed -i 's/</\<\;/g' $filename
sed -i '/^[^>]/ s/>/\>\;/g' $filename
# comment -> if arg1 is "-c" then include all custom comments else remove all comments /* article 1 */
if [ "$1" = "-c" ]; then
sed -i 's/\s\/\*\(.*\)\*\//\n<!-- \1 -->/g' $filename
else
com="false"
sed -i 's/\s\/\*.*\*\/$//g' $filename
sed -i '/^\/\*.*\*\/$/d' $filename
fi
# cleaning up double .// to /
sed -i "/^+.*head$/,/^-.*head$/ s/\.\/\//.\//g" $filename
sed -i "/^+.*navigation$/,/^-.*navigation$/ s/\.\/\//.\//g" $filename
sed -i "/^+.*script$/,/^-.*script$/ s/\.\/\//.\//g" $filename
# inline call-script substitution
sed -i 's/^\.call-script:\s\(.*\)$/\t<script>\1<\/script>/g' $filename
# script-src substitution
sed -i 's/^\.script:\s\(.*\)/\t<script src="\1"><\/script>/g' $filename
# additional script section will be removed
sed -i '/^+++.*add$/d' $filename
sed -i '/^---.*add$/d' $filename
# add tab inside script section
sed -i '/^+.*script$/,/^-.*script/ s/\(^[^+,^-].*\)/\t\1/g' $filename
# top section
sed -i '1s/\(.*\)/<!DOCTYPE html>\n<html lang="en">\n\1/' $filename
# head section
sed -i '/^+.*head$/,/^-.*head$/ s/\.title:\s\(.*\)/\t<title>\1<\/title>\n\t<meta charset="UTF-8">\n\t<meta name="viewport" content="width=device-width, initial-scale=0.9">\n\t<meta name="theme-color" content="#f8f8eb">/g' $filename
sed -i '/^+.*head$/,/^-.*head$/ s/\.author:\s\(.*\)/\t<meta name="author" content="\1">/g' $filename
sed -i '/^+.*head$/,/^-.*head$/ s/\.description:\s\(.*\)/\t<meta name="description" content="\1">/g' $filename
sed -i '/^+.*head$/,/^-.*head$/ s/\.style:\s\(.*\)/\t<link rel="stylesheet" href="\1" type="text\/css">/g' $filename
sed -i '/^+.*head$/,/^-.*head$/ s/\.icon:\s\(.*\)/\t<link rel="icon" href="\1">/g' $filename
sed -i '/^+.*head$/,/^-.*head$/ s/\.name-generator:\s\(.*\)/\t<meta name="generator" content="\1">/g' $filename
sed -i '/^+.*head$/,/^-.*head$/ s/\.canonical-link:\s\(.*\)/\t<link rel="canonical" href="\1">/g' $filename
sed -i '/^+.*head$/,/^-.*head$/ s/^$/<!-- blank line -->/g' $filename
sed -i 's/^+.*head$/<!-- header section begin -->\n<head>/g' $filename
sed -i 's/^-.*head/<\/head>\n<!-- header section end -->/g' $filename
# navigation section
sed -i '/^+.*navigation$/,/^-.*navigation$/ s/\.homepage:\s\[\([^]]*\)\](\([^)]*\))/\t\t<a href="\2"><home>\1<\/home><\/a>/g' $filename
sed -i '/^+.*navigation$/,/^-.*navigation$/ s/\.navmenu:\s\(.*\)/\t<\/li><li class="dropdown">\n\t<button class="dropbtn">\n\t\t<div class="index">\1<\/div>\n\t<\/button>\n\t<div class="dropdown-content">/g' $filename
# transform urls
sed -i '/^+.*navigation$/,/^-.*navigation$/ s/\.navpage:\s\[\([^]]*\)\](\([^)]*\))/\t\t<a href="\2">\1<\/a>/g' $filename
sed -i '/^+.*navigation$/,/^-.*navigation$/ s/\.backpage:\s\[\([^]]*\)\](\([^)]*\))/\t\t<\/div>\n\t<li><a href="\2"><back>\1<\/back><\/a>\n<\/li>\n<\/ul>/g' $filename
# navigation tags transform
sed -i '/^+.*navigation$/,/^-.*navigation$/ s/^$/<!-- blank line -->/g' $filename
sed -i 's/^+.*navigation$/<!-- navigation section begin -->\n<body>\n<header role="banner">\n<nav role="navigation">\n\n<ul class="navigation">\n\t<li>/g' $filename
sed -i 's/^-.*navigation$/<\/nav>\n<\/header>\n<!-- navigation section end -->/g' $filename
# convert <hr> tags from .hr
sed -i 's/^\.hr$/<hr>/g' $filename
# convert <br> tags from .br
sed -i 's/^\.br$/<br>/g' $filename
# get directory structure
echo $filename | grep -q "/"
if [ "$?" = 0 ]; then
dirr="$(echo "$current" | sed 's/\(.*\)\/.*/\1/g' | \
sed 's/\([[:alpha:]]\|[[:alnum:]]\|[[:digit:]]\)*/..\//g; s/\/\//\//g')"
else
dirr="."
fi
# noscript section
sed -i "/^+.*main$/ i <div id="switch" class="inner-switch">\n\t<span id="sword">λ<\/span>\n<\/div>\n<noscript>\n\t<style type="text\/css" media="all">\n\t@import '$dirr\/css\/dark.css' screen and (prefers-color-scheme: dark);\n\t.inner-switch {\n\tdisplay: none;\n}\n\t<\/style>\n<\/noscript>\n" $filename
# card section start
sed -i '/^+.*card$/,/^-.*card$/ s/^\.date:\s\(.*\)/<div class="card">\n<div class="date">\1<\/div>/g' $filename
sed -i '/^+.*card$/,/^-.*card$/ s/^\.article:\s\[\([^]]*\)\](\([^)]*\))/<h2><a href="\2">\1<\/a><\/h2>/g' $filename
sed -i '/^+.*card$/,/^-.*card$/ s/^\.describe:\s\(.*\)/<p>\1<\/p>\n<\/div>/g' $filename
# card tags transform
sed -i 's/^+.*card$/<div class="grid-container">\n/g' $filename
sed -i 's/^-.*card$/<\/div>/g' $filename
# card section end
# table section start
awk -i inplace '
/^$/ { blank++ }
blank && /^\.th:\s/ { blank=0; block++; print "\t<tr>" }
block && blank { block=0; print "\t</tr>" }
/^./ { blank=0 }
{ print }' $filename
sed -i '/^+.*table$/,/^-.*table$/ s/\.th:\s\(.*\)/\t\t<th>\1<\/th>/g' $filename
awk -i inplace '
/^$/ { blank++ }
blank && /^\.td:\s/ { blank=0; block++; print "\t<tr>" }
block && blank { block=0; print "\t</tr>" }
/^./ { blank=0 }
{ print }' $filename
sed -i '/^+.*table$/,/^-.*table$/ s/\.td:\s\(.*\)/\t\t<td>\1<\/td>/g' $filename
# table sections replacement
sed -i 's/^+.*table$/<center>\n<table>/g' $filename
sed -i 's/^-.*table$/<\/table>\n<\/center>\n<br>/g' $filename
# table section end
# footer section
#sed -i '/^+.*footer$/,/^-.*footer$/ s/\.class:\s\(.*\)/<footer class="\1" role="contentinfo">\n<div class="pin">📎<\/div>/g' $filename
#sed -i '/^+.*footer$/,/^-.*footer$/ s/\[\([^]]*\)\](\([^)]*\))/<a href="\2" target="_blank" rel="nofollow">\1<\/a>/g' $filename
# removing class section if mentioned
sed -i '/^+.*footer$/,/^-.*footer$/ s/^\.class:.*//g' $filename
sed -i '/^+.*footer$/,/^-.*footer$/ s/\.message:\s\(.*\)/<center><p>\1<\/p><\/center>\n/g' $filename
sed -i 's/^+.*footer$/<!-- footer section begin -->/g' $filename
sed -i 's/^-.*footer$/<!-- footer section end -->/g' $filename
# end section
sed -i 's/^+.*script$/<script>/g' $filename
sed -i 's/^-.*script$/<\/script>/g' $filename
# Add current Month date, year
today="$(date +%B' '%e', '%Y)" && \
sed -i "s/\[\.today\]/$today/g" $filename
# markdown style video addition
sed -i 's/\!\!\[\([^]]*\)\](\([^)]*\))/<center>\n\t<video title="\1" controls>\n\t\t<source src="\2">\n\t<\/video>\n<\/center>/g' $filename
# markdown style image addition
sed -i 's/^\!\[\([^]]*\)\](\([^)]*\))/<center>\n\t<img loading="lazy" class="pimg" src="\2" alt="\1">\n<\/center>/g' $filename
# custom markdown style next page
sed -i 's/^\.next\[\([^]]*\)\](\([^)]*\))/<center><div class="next_page"><a href="\2" rel="nofollow">\1<\/a><\/div><\/center>/g' $filename
# main section
sed -i '/^+.*main$/,/^-.*main$/ s/^\.ce\sheader1:\s\(.*\)/<center><h1>\1<\/center><\/h1>/g' $filename
sed -i '/^+.*main$/,/^-.*main$/ s/^\.ce\sheader2:\s\(.*\)/<center><h2>\1<\/center><\/h2>/g' $filename
sed -i '/^+.*main$/,/^-.*main$/ s/^\.ce\sheader3:\s\(.*\)/<center><h3>\1<\/center><\/h3>/g' $filename
sed -i '/^+.*main$/,/^-.*main$/ s/^\.ce\sheader4:\s\(.*\)/<center><h4>\1<\/center><\/h4>/g' $filename
# Header tag substitution with class and parameter with {whatever="whatever"} notice the double quote around whatever
sed -i 's/^#\s{\(.*\)="\(.*\)"}\s\(.*\)/<h1 \1="\2">\3<\/h1>/g' $filename
sed -i 's/^##\s{\(.*\)="\(.*\)"}\s\(.*\)/<h2 \1="\2">\3<\/h2>/g' $filename
sed -i 's/^###\s{\(.*\)="\(.*\)"}\s\(.*\)/<h3 \1="\2">\3<\/h3>/g' $filename
sed -i 's/^####\s{\(.*\)="\(.*\)"}\s\(.*\)/<h4 \1="\2">\3<\/h4>/g' $filename
sed -i 's/^#####\s{\(.*\)="\(.*\)"}\s\(.*\)/<h5 \1="\2">\3<\/h5>/g' $filename
# Normal header tag substitution
sed -i 's/^#\s\(.*\)/<h1>\1<\/h1>/g' $filename
sed -i 's/^##\s\(.*\)/<h2>\1<\/h2>/g' $filename
sed -i 's/^###\s\(.*\)/<h3>\1<\/h3>/g' $filename
sed -i 's/^####\s\(.*\)/<h4>\1<\/h4>/g' $filename
sed -i 's/^#####\s\(.*\)/<h5>\1<\/h5>/g' $filename
# Cover image substitution within paragraphs
sed -i 's/^\.cover-img:\s\(.*\)/<img class="cover" src="\1">/g' $filename
# Image substitution within paragraphs, (png or svg or jpeg or jpg or gif files)
sed -i 's/^\.img:\sclass=\(".*"\)\s\(.*\(\.png\|\.jpg\|\.jpeg\|.gif\|\.svg\)\)\s\(.*\)/<center><img class=\1 src="\2" alt="\4" loading="lazy"><\/center>/g' $filename
# if class is not mentioned, fallback to noclass
sed -i 's/^\.img:\s\(.*\(\.png\|\.jpg\|\.jpeg\|.gif\|\.svg\)\)\s\(.*\)/<center><img src="\1" alt="\3"><\/center>/g' $filename
# if img-def is mentioned, then use class pimg
sed -i 's/^\.pimg:\s\(.*\(\.png\|\.jpg\|\.jpeg\|.gif\|\.svg\)\)\s\(.*\)/<center><img class="pimg" src="\1" alt="\3"><\/center>/g' $filename
# for explicitly mentioning classes without alt text
sed -i 's/^\.img:\sclass=\(".*"\)\s\(.*\(\.png\|\.jpg\|\.jpeg\|.gif\|\.svg\)\)$/<center><img class=\1 src="\2"><\/center>/g' $filename
# Caption under image substitution within paragraph
sed -i 's/^\.caption:\s\(.*\)/\n<div class="caption">\1<\/div>/g' $filename
# Url substitution
sed -i 's/\[\([^]]*\)\](\([^)]*\))/<a href="\2" rel="nofollow">\1<\/a>/g' $filename
# Nested blockquote substitution (experimental)
sed -i 's/^>>\s\(.*\)/\t<blockquote>\n\t<p>\1<\/p>\n\t<\/blockquote>/g' $filename
# Blockquote substitution
awk -i inplace '
/^$/ { blank++ }
blank && /^>/ { blank=0; block++; print "<blockquote>" }
block && blank { block=0; print "</blockquote>" }
/^./ { blank=0 }
{ print }' $filename
sed -i '/^<blockquote>$/,/^<\/blockquote>$/ s/^>\s#\s\(.*\)/\t<h1>\1<\/h1>/g' $filename
sed -i '/^<blockquote>$/,/^<\/blockquote>$/ s/^>\s##\s\(.*\)/\t<h2>\1<\/h2>/g' $filename
sed -i '/^<blockquote>$/,/^<\/blockquote>$/ s/^>\s###\s\(.*\)/\t<h3>\1<\/h3>/g' $filename
sed -i '/^<blockquote>$/,/^<\/blockquote>$/ s/^>\s\(.*\)/\t<p>\1<\/p>/g' $filename
# Unordered list substitution with bulleted [*] markers
awk -i inplace '
/^$/ { blank++ }
blank && /^\*\s/ { blank=0; block++; print "<ul>" }
block && blank { block=0; print "</ul>" }
/^./ { blank=0 }
{ print }' $filename
sed -i '/^<ul>$/,/^<\/ul>$/ s/^\*.\(.*\)/\t<li>\1<\/li>/g' $filename
# Unordered list with dashes [-] which will later turn in bullets in html
awk -i inplace '
/^$/ { blank++ }
blank && /^-\s/ { blank=0; block++; print "<ul>" }
block && blank { block=0; print "</ul>" }
/^./ { blank=0 }
{ print }' $filename
sed -i '/^<ul>$/,/^<\/ul>$/ s/^-\s\(.*\)/\t<li>\1<\/li>/g' $filename
# Unordered list with # instead of bullet points
awk -i inplace '
/^$/ { blank++ }
blank && /^#\.\s/ { blank=0; block++; print "<ul class=\"ull\">" }
block && blank { block=0; print "</ul>" }
/^./ { blank=0 }
{ print }' $filename
sed -i '/^<ul class="ull">$/,/^<\/ul>$/ s/^\#\.\s\(.*\)/\t<li>\1<\/li>/g' $filename
# Ordered list substitution for numbered lines
awk -i inplace '
/^$/ { blank++ }
blank && /^[[:digit:]]\./ { blank=0; block++; print "<ol>" }
block && blank { block=0; print "</ol>" }
/^./ { blank=0 }
{ print }' $filename
sed -i '/^<ol>$/,/^<\/ol>$/ s/^[[:digit:]]..\(.*\)/\t<li>\1<\/li>/g' $filename
# Ordered list substitution for numbered lines
awk -i inplace '
/^$/ { blank++ }
blank && /^[a-z]\.\s/ { blank=0; block++; print "<ol>" }
block && blank { block=0; print "</ol>" }
/^./ { blank=0 }
{ print }' $filename
sed -i '/^<ol>$/,/^<\/ol>$/ s/^[a-z]\.\s\(.*\)/\t<li>\1<\/li>/g' $filename
# for no code copy section
sed -i 's/^```[[:alpha:]]\+$/<pre>\n\t<code>/g' $filename
sed -i 's/^```no$/<pre>\n\t<code>/g' $filename
sed -i 's/^```$/\t<\/code>\n<\/pre>/g' $filename
# tmp digit character replacement instead of ..*$ because code block raw text view
sed -i 's/^```[[:digit:]]\+$/<pre>\n\t<code>/g' $filename
sed -i 's/^```$/\t<\/code>\n<\/pre>/g' $filename
# code href
echo $filename | grep -q "/"
if [ "$?" = 0 ]; then
name_sub="$(echo "$1" | sed 's/.*\/\(.*\).md$/\1.html/g')"
sed -i "s/^\.\(code[[:digit:]]\+\)$/<a class='btn' href='code\/$name_sub-\1.txt'>view raw<\/a>/g" $filename
else
sed -i "s/^\.\(code[[:digit:]]\+\)$/<a class='btn' href='code\/$filename-\1.txt'>view raw<\/a>/g" $filename
fi
# Paragraph substitution
sed -i '/^+.*main$/,/^-.*main$/ s/\(^[^<,^>,\t,#,^+,^-].*\)/<p>\1<\/p>/g' $filename
# Strike through text within paragraphs and blockquotes
sed -i '/^<p>\|^\t<p>/,/<\/p>$/ s/~~\([^~]*\)~~/<strike>\1<\/strike>/g' $filename
# Bold text substitution specific to paragraphs and blockquotes
sed -i '/^<p>\|^\t<p>/,/<\/p>$/ s/\*\*\([^.*]*\)\*\*/<b>\1<\/b>/g' $filename
# Bold-italic text substitution specific to paragraphs and blockquotes
sed -i '/^<p>\|^\t<p>/,/<\/p>$/ s/\*\*\*\([^.*]*\)\*\*\*/<i><b>\1<\/i><\/b>/g' $filename
# Bold-italic text with underscore syntaxes
sed -i '/^<p>\|^\t<p>/,/<\/p>$/ s/___\([^_]*\)___/<b><i>\1<\/b><\/i>/g' $filename
# Bold text with underscore syntaxes
sed -i '/^<p>\|^\t<p>/,/<\/p>$/ s/__\([^_]*\)__/<b>\1<\/b>/g' $filename
# Italic text with underscore syntaxes
sed -i '/^<p>\|^\t<p>/,/<\/p>$/ s/_\([^_]*\)_/<i>\1<\/i>/g' $filename
# Underline text substitution specific to paragraphs and blockquotes
sed -i '/^<p>/,/<\/p>$/ s/,,,\([^,]*\),,,/<u>\1<\/u>/g' $filename
# Italic text substitution specific to paragraphs blockquotes
sed -i '/^<p>\|^\t<p>/,/<\/p>$/ s/\*\([^.*]*\)\*/<i>\1<\/i>/g' $filename
# Code snippet substitution within paragraphs and blockquotes
sed -i '/^<p>\|^\t<p>/,/<\/p>$/ s/`\([^`]*\)`/<code>\1<\/code>/g' $filename
# Literal backtick within a code paragraphs and blockquotes
#sed -i '/^<p>\|^\t<p>/,/<\/p>$/ s/\\<code>\([^.*]*\)\\<\/code>/`\1`/g' $filename
# reverting of <code> tags within codeblocks
#sed -i '/^\t<code>$/,/^\t<\/code>$/ s/<code>\([^.*]*\)<\/code>/`\1`/g' $filename
# deleting starting tabs within code blocks
sed -i '/^\t<code>$/,/^\t<\/code>$/ s/^\t//g' $filename
# main tag transformation
sed -i 's/^+.*main$/<!-- main section begin -->\n<main id="main" role="main">/g' $filename
sed -i 's/^-.*main$/<!-- main section end -->/g' $filename
# Cleaning up
sed -i '/^>$/d' $filename
sed -i '/^<p>.<\/p>$/d' $filename
# remove all comments if com=false (-c flag isn't invoked)
[ "$com" = "false" ] && sed -i '/^<!.*-->$/d' $filename
# ending tags
echo "</body>" >> $filename
echo "</html>" >> $filename
}
## post articles -> main post function -> will be called later
main_post() {
# functions begin
empty() {
echo "value can not be empty, exiting..."
exit 1
}
invalid() {
echo "invalid argument, exiting..."
exit 1
}
# functions end
echo
read -p "Provide a meta-name generator[optional]: " ng
arg="$ng" && skip
echo
read -p "Provide a canonical link[optional]: " canon
arg="$canon" && skip
echo
read -p "Skip for default css, or put the path/style.css for custom css or type n for no css: " ccss
arg="$ccss" && skip
echo
read -p "Skip for default js or put path/script.js for custom js or type n for no js: " cjs
arg="$cjs" && skip
echo
read -p "Use a footer or no, default is use footer [y/n]: " footer
val="$footer" && skip
echo
read -p "make file into .html when done? [y/n]: " generate
val="$generate" && skip
touch $current
# check if editor variable is defined
[ -z "$EDITOR" ] && \
echo "Editor global variable is not set" && \
exit 1
# invoke editor variable if prefix is set
$EDITOR $current
# if file is empty
if [ ! -s "$current" ]; then
echo "$current is an empty file, exiting..."
exit 1
fi
# for directory
echo $current | grep -q "/"
if [ "$?" = 0 ]; then
dirr="$(echo "$current" | sed 's/\(.*\)\/.*/\1/g' | \
sed 's/\([[:alpha:]]\|[[:alnum:]]\|[[:digit:]]\)*/..\//g; s/\/\//\//g')"
else
dirr="."
fi
# main section begin
sed -i '1i ++++++++++++++++main' $current
# if card section found
grep -q "^\.date:\s" $current && grep -q "^\.article:\s" $current && grep -q "^\.describe:\s" $current
case "$?" in
0 ) sed -i '0,/^\.date:\s/{s/^\.date:\s\(.*\)/+++++++++++++++++card\n\n.date: \1/g}' $current
ce="$(grep -n '^\.describe:\s' $current | sed -n '$p' | cut -f1 -d ':')"
sed -i "$ce,/^\.describe:\s/{s/^\.describe:\s\(.*\)/.describe: \1\n\n-----------------card/g}" $current
;;
* ) echo "using default format instead of card format, because card format doesn't seem to be mentioned"
break
;;
esac
# end card section
# if table section found
grep -q "^\.th:\s" $current && grep -q "^\.td:\s" $current
case "$?" in
0 ) sed -i '0,/^\.th:\s/{s/^\.th:\s\(.*\)/+++++++++++++++++table\n\n.th: \1/g}' $current
ce="$(grep -n '^\.td:\s' $current | sed -n '$p' | cut -f1 -d ':')"
sed -i "$ce,/^\.td:\s/{s/^\.td:\s\(.*\)/.td: \1\n\n----------------table/g}" $current
;;
* ) echo "table format doesn't seem to be included, skipping this method"
break
;;
esac
#end table section
sed -i '$a ----------------main' $current
# main section end
# footer section
case "$footer" in
""|" "|y|Y ) grep -q "^+.*footer$" $config_file && grep -q "^-.*footer$" $config_file
if [ "$?" -ne 0 ]; then
echo "footer section is not found in $config_file, exiting..." && \
exit 1;
fi
sed -n '/^+.*footer$/,/^-.*footer$/p' $config_file >> $current
;;
n ) echo "no footer, skipping adding a footer"
break
;;
* ) echo "invalid argument for footer, exiting..."
exit 1
;;
esac
#footer section end
# script section
case "$cjs" in
""|" "|d|D ) grep -q "^+.*script$" $config_file && grep -q "^-.*script$" $config_file
if [ "$?" -ne 0 ]; then
echo "script section is not mentioned in $config_file, exiting..." && \
exit 1;
fi
if [ ! -f "js/toggle.js" ]; then
echo "js/toggle.js file not found, exiting..." && \
exit 1;
fi
sed -n '/^+.*script$/,/^-.*script$/p' $config_file >> $current
sed -n '/^+.*add$/,/^-.*add$/p' $config_file >> $current
echo $dirr | grep -q "/"
if [ "$?" = 0 ]; then
ddd="$(echo $dirr | sed 's/\//\\\//g')"
sed -i "/^+.*add$/,/^-.*add$/ s/^\.script:\s\(.*\)/.script: $ddd\/\1/g" $current
else
sed -i "/^+.*add$/,/^-.*add$/ s/^\.script:\s\(.*\)/.script: $dirr\/\1/g" $current
fi
;;
n ) echo "no js, skipping js"
break
;;
* ) [ -f "$cjs" ] && \
sed -i 1"i .script: $cjs" $current || \
echo "$cjs file not found"
;;
esac
# script section end
# head section
sed -i 1'i -------------------head' $current
[ -n "$canon" ] && \
sed -i 1"i .canonical-link: $canon" $current
[ -n "$ng" ] && \
sed -i 1"i .name-generator: $ng" $current
# css section
case "$ccss" in
""|" " ) if [ -f "css/main.css" ]; then
if [ -f "css/maind.css" ]; then
sed -i 1"i .style: $dirr/css\/main.css" $current && sed -i 1"i .style: $dirr/css\/maind.css" $current
fi
else
echo "default css file not found"
exit 1
fi
;;
n ) echo "no css file, skipping css file entry"
break
;;
* ) [ -f "$ccss" ] && \
sed -i 1"i .style: $ccss" $current || \
echo "$ccss file not found"
;;
esac
# css section end
# wont run if previous sections fail
[ -n "$describe" ] && \
sed -i 1"i .description: $describe" $current
[ -n "$author" ] && \
sed -i 1"i .author: $author" $current
[ -n "$title" ] && \
sed -i 1"i .title: $title" $current
sed -i 1'i +++++++++++++++++head' $current
# nav section
nav_check
# gets the part in navigation section and puts it in main file
sed -n '/^+.*navigation$/,/^-.*navigation$/p' $config_file | \
xargs -I '{}' sed -i '/^+.*main$/i {}' $current
# escape forward slashes transformation
echo $dirr | grep -q "/"
if [ "$?" = 0 ]; then
dnav="$(echo $dirr | sed 's/\//\\\//g')"
sed -i "/^+.*navigation/,/^-.*navigation$/ s/^\.navpage:\s\[\(.*\)\](\(.*\))/.navpage: [\1]($dnav\/\2)/g" $current
sed -i "/^+.*navigation/,/^-.*navigation$/ s/^\.homepage:\s\[\(.*\)\](\(.*\))/.homepage: [\1]($dnav\/\2)/g" $current
else
sed -i "/^+.*navigation/,/^-.*navigation$/ s/^\.navpage:\s\[\(.*\)\](\(.*\))/.navpage: [\1]($dirr\/\2)/g" $current
sed -i "/^+.*navigation/,/^-.*navigation$/ s/^\.homepage:\s\[\(.*\)\](\(.*\))/.homepage: [\1]($dirr\/\2)/g" $current
fi
# if page name is about.md then change backpage to about
echo $current | grep -q "about.md"
[ "$?" = 0 ] && \
sed -i '/^+.*navigation$/,/^-.*navigation$/ s/^\.backpage:\s.*/.backpage: [about](.\/about.html)/g' $current
# if page has contact.md then change backpage to contact
echo $current | grep -q "contact.md"
[ "$?" = 0 ] && \
sed -i '/^+.*navigation$/,/^-.*navigation$/ s/^\.backpage:\s.*/.backpage: [contact](.\/contact.html)/g' $current
# add an extra line for aesthetic reasons
sed -i 's/\(--.*\)$/\1\n/g' $current
# generate to html if value is yes
case "$generate" in
y|Y|yes|Yes|""|" " ) # call convert function
main_generate $current
;;
n|N|No|no ) echo "file left to manually generate to html. Run 'sh main.sh html $current'"
;;
* ) echo "Invalid value, file $current not generated to html"
break
;;
esac
}
## post article
post() {
empty_check() {
case "$val" in
""|" "|" " ) empty
;;
* ) break
;;
esac
}
skip() {
case "$arg" in
"" ) echo "using defaults for this"
break
;;
" "|" "|" " ) invalid
;;
* ) break
;;
esac
}
# check if config file exists
check_config
echo "Generate new article"
echo
# check for article title
ask_title
echo
# check for author
ask_author
echo
# check for description
ask_describe
echo
# ask for file name
ask_name
# run main post function
main_post
}
## add a new post
add_post() {
# functions begin
empty_check() {
case "$val" in
""|" "|" " ) empty
;;
* ) break
;;
esac
}
skip() {
case "$arg" in
"" ) echo "using defaults for this"
break
;;
" "|" "|" " ) invalid
;;
* ) break
;;
esac
}
date_eval() {
case "$daate" in
""|" "|d ) daaate="$(date +%B' '%e', '%Y)"
;;
c ) read -p "Enter custom date: " daate
daaate="$daate"
;;
* ) "Ignoring date value"
break
;;
esac
}
# functions end
# check if config file exists
check_config
# if found, print below message
echo "$config_file found"
echo "Adding new article"
echo
read -p "dirname and filename.md to add [eg -> bash/new.md]: " file
val="$file" && empty_check
echo
read -p "Use [d]efault date or [c]ustom date[d/c]: " daate
date_eval
echo
read -p "Add a title: " title
val="$title" && empty_check
echo
read -p "Write a brief description about the article: " describe
val="$describe" && empty_check
echo
file_dir="$(dirname $file)"
if [ ! -f "$file_dir/base.md" ]; then
echo "$file_dir/base.md not found, perhaps you should create the file first. Exiting..." && \
return 1;