]> zdv2.bktei.com Git - BK-2020-03.git/blobdiff - user/mw_wc2sp.sh
feat(user/mw_wc2sp.sh):Use top and bottom nav links
[BK-2020-03.git] / user / mw_wc2sp.sh
index 421adb702cc9be53a603c30c7aa0b3215b73b60d..467df7a70c54f82e9fa0f927fda3b4734f0086e1 100755 (executable)
@@ -4,14 +4,16 @@
 # Input:  arg1  path  input wikicode file
 # Output: files       wikicode file tree
 # Depends: Bash 5.1.16, GNU Coreutils 8.32
-# Version: 0.0.1
+# Version: 0.4.0
 
 re_sp='^(<!-- @subpage:)(.*)([ ]*-->)$'; # subpage marker pattern
 d_out=./wikicode/; # default output dir
-f_spl="subpage_list.txt";
-p_spl="${d_out}/${f_spl}";
-f_splv="subpage_list_validated.txt";
-p_splv="${d_out}/${f_splv}";
+f_spl="subpage_list.txt"; # subpage title list
+p_spl="${d_out}/subpages/${f_spl}";
+f_splv="subpage_list_validated.txt"; # subpage title list (validated)
+p_splv="${d_out}/subpages/${f_splv}";
+f_splwc="subpages.wc"; # subpage list wikicode
+p_splwc="${d_out}/${f_splwc}";
 
 yell() { echo "$0: $*" >&2; } # print script path and all args to stderr
 die() { yell "$*"; exit 111; } # same as yell() but non-zero exit status
@@ -187,18 +189,18 @@ assemble_subpage_ftree() {
 
     yell "STATUS:Running assemble_subpage_ftree()."; # debug
 
-    spc_path="${d_out}/presubpage.content"; # default destination for content before subpage detected
+    spc_path="${d_out}/subpages/presubpage.content"; # default destination for content before subpage detected
     
     ## Process input line-by-line
     while read -r line; do
-        #declare -p line re_sp; # debug
+        # declare -p line re_sp; # debug
         ### Check for subpage marker
         if [[ "$line" =~ $re_sp ]]; then
             #### Identify new subpage path
             sp_path="$(echo "$line" | sed -E -e "s/${re_sp}/\2/" -e 's/[ ]*$//'; )";
             # declare -p sp_path; # debug
             #### Update subpage content file path
-            spc_path="${d_out}/${sp_path}.content";
+            spc_path="${d_out}/subpages/${sp_path}.content";
             spc_dir="$(dirname "$spc_path"; )";
             #declare -p spc_path spc_dir; # debug
             #### Prepare file destination
@@ -220,6 +222,40 @@ assemble_subpage_ftree() {
 
     yell "STATUS:Finished assemble_subpage_ftree()."; # debug
 }; # process input wikicode into subpage content files and subpage list
+print_wc_content() {
+    # Input: var  p_spc  path  subpage content
+    # Output: stdout
+    printf -- "\n----<onlyinclude>\n";
+    cat "$p_spc";
+    printf -- "\n</onlyinclude>----\n";
+}; # print wikicode content
+print_wc_footer() {
+    # Output: stdout
+    printf -- "\n==References==\n<references />\n"
+    printf -- "\n==Footnotes==\n<references group=fn />\n";
+    printf -- "\n==Comments==\n<references group=cmt />\n";
+    printf -- "\n<!-- End of Page -->\n";
+    printf -- "\n";
+}; # print wikicode footer
+print_wc_nav() {
+    # Desc: Print navigation wikilinks
+    # Input:  var  lprev
+    #         var  lnext
+    #         var  link_prev
+    #         var  link_next
+    # Output  stdout
+
+    # Print navigation link wikicode
+    if [[ -z "$lprev" ]]; then
+        printf "\n[[%s|Next]], [[../|Up]]\n" "$link_next";
+    elif [[ -n "$lnext" ]]; then
+        printf "\n[[%s|Next]], [[%s|Previous]], [[../|Up]]\n" "$link_next" "$link_prev";
+    elif [[ -z "$lnext" ]]; then
+        printf "\n[[%s|Previous]], [[../|Up]]\n" "$link_prev";
+    else
+        yell "FATAL:Here be dragons.";
+    fi;
+}; # print wikicode navigation links
 create_output_wikicode() {
     # Desc: Use subpage list and subpage content files to create
     #   output subpage wikicode.
@@ -252,6 +288,10 @@ create_output_wikicode() {
         die "FATAL:Different line counts for subpage lists:$(declare -p lc_spl lc_splv;)";
     fi;
     declare -p lc_spl lc_splv; # debug
+
+    # Initialize subpage list wikicode file
+    must touch "$p_splwc";
+    printf "==Stats==\n\n==Subpages==\n" >> "$p_splwc";
     
     # Read content files according to subpage list file    
     #   Note: $i corresponds to “next” line ($lnext). Therefore, use
@@ -263,7 +303,7 @@ create_output_wikicode() {
         
         # Check subpage content files
         f_spc="${lines_spl[i-1]}.content"; 
-        p_spc="${d_out}/${f_spc}";
+        p_spc="${d_out}/subpages/${f_spc}";
         declare -p f_spc p_spc;
         ## Exit if subpage content file missing
         if [[ "$i" -gt 0 ]] && [[ ! -f "$p_spc" ]]; then
@@ -271,9 +311,13 @@ create_output_wikicode() {
 
         # Prepare output subpage wikicode files
         f_spwc="${lines_splv[i-1]}.wc";
-        p_spwc="${d_out}/${f_spwc}"; # use validated subpage name
+        p_spwc="${d_out}/subpages/${f_spwc}"; # use validated subpage name
         declare -p f_spwc p_spwc; # debug
-        if [[ "$i" -gt 0 ]]; then must touch "$p_spwc"; fi;
+        if [[ "$i" -gt 0 ]]; then
+            must touch "$p_spwc";
+            ## Append subpage list wikicode file
+            printf "* [[/%s]]\n" "${lines_splv[i-1]}" >> "$p_splwc";
+        fi;
 
         # Advance input lines
         lprev="$lcurr";
@@ -311,25 +355,11 @@ create_output_wikicode() {
         link_next+="$(prune_path_rootside "$lnext" "$fork_level_next")";
         link_prev+="$(prune_path_rootside "$lprev" "$fork_level_prev")";
 
-        # Print navigation link wikicode
-        if [[ -z "$lprev" ]]; then
-            printf "[[%s|Next]], [[../|Up]]\n" "$link_next" >> "$p_spwc";
-        elif [[ -n "$lnext" ]]; then
-            printf "[[%s|Next]], [[%s|Previous]], [[../|Up]]\n" "$link_next" "$link_prev" >> "$p_spwc";
-        elif [[ -z "$lnext" ]]; then
-            printf "[[%s|Previous]], [[../|Up]]\n" "$link_prev" >> "$p_spwc";
-        else
-            yell "FATAL:Here be dragons.";
-        fi;
-
         # Print subpage content
-        printf -- "\n----<onlyinclude>\n" >> "$p_spwc";
-        cat "$p_spc" >> "$p_spwc";
-        printf -- "\n</onlyinclude>----\n" >> "$p_spwc";
-        printf -- "\n==References==\n<references />\n" >> "$p_spwc";
-        printf -- "\n==Footnotes==\n<references group=fn />\n" >> "$p_spwc";
-        printf -- "\n==Comments==\n<references group=cmt />\n" >> "$p_spwc";
-        printf -- "\n"; >> "$p_spwc";
+        print_wc_nav >> "$p_spwc";
+        print_wc_content >> "$p_spwc";
+        print_wc_nav >> "$p_spwc";
+        print_wc_footer >> "$p_spwc";
 
         declare -p i lprev lcurr lnext lprev_hier lcurr_hier lnext_hier; # debug
         declare -p fork_level_next fork_level_prev relups_next relups_prev; # debug
@@ -337,6 +367,9 @@ create_output_wikicode() {
         printf "====================\n" # debug
     done;
 
+    # Add footer to subpage list wikicode file
+    print_wc_footer >> "$p_splwc";
+    
     yell "STATUS:Finished create_output_wikicode()."; # debug
 }; # generate output subpage wikicode
 main() {