text.2.wiki.awk

#:       C:\#\awk\lib\text.2.wiki.awk
#:       2012-09-23   13:39:45       #:rod.t_2012
#:       This file is http://awk.freeshell.org/text.2.wiki.awk       (g0ph3r_)
#:       Converts text to a format for the awk wiki:       http://awk.freeshell.org
#:       See: http://awk.freeshell.org/How_to_edit
#:       Adds a \\ to end of every line to force line break
#:       except those ending in ` or \\ (removes the `)
#:       Converts tabs \t to four spaces.
#:       Converts n spaces to (n -1)   followed by a space.
#:       Converts literal \240 to the character \240 == \xa0 - non breaking space.
#:       Prints results to stdout and copies to the clipboard.
#: Remember to escape | \\ // * # == etc. eg {{{| }}}
#:       Use **{{&#123|}}&#125** to make bold; [[..?|link]] use   for spaces.
#:       To escape text; use {{&#123 for .txt files or #{{&#123 for .awk files
#:       or ::{{&#123 for .cmd files or ;{{&#123 for .ini files.
#:       (end with the corresponding ..}}&#125)
#:       %gawk% --lint -f C:\#\awk\lib\text.2.wiki.awk -v _prefix="@rem |<!--"
#:       -v _clip=1 -v _debug=1 -- C:\#\awk\#awk_wiki\test.text.2.wiki.awk.txt
#: test.text.2.wiki.awk.txt == http://awkpaste.blisted.org/cgi/paste.cgi?id=2874&#38;view=text
#:       insert a -v _clip=1 to copy results to the clipboard
##   _____________________________________________

BEGIN   {

      $1 = _in_escape = t = "";       _esc1 = "{\173{";       _esc2 = "}\175}";
      _splat2 = "**";       _bs2 = "\\\\";
      _prefix = _prefix "|#|::|;||";
      n = split(_prefix, B, "|");

      if(_debug)   for(i in B)   { print(_qt_(++j, "B[]", i, B[i]));   fflush(); }
      for(i in B)   {
              A[(B[i] _esc1)] = _esc1;
              A[(B[i] _esc2)] = _esc2;
      }
      if(_debug)   for(i in A)   { print(_qt_(++k, "A[]", i, A[i]));   fflush(); }
}

{
      for(i in A)   if(tolower($1) == i)   {
              if((! _in_escape) && (A[i] == _esc1))   {
                      _in_escape = 1;
                      $1 = "";
#:                       t = t ? (t "\n" A[i] "\n") : (A[i] "\n");
                      if(_debug)   { print(_qt_("_in_escape:", _in_escape, t, "end"));   fflush(); }
                      if($0 == "")   next;
              }   else   if(_in_escape && (A[i] == _esc2))   {
                      _in_escape = "";
                      $1 = "";
#:                       t = t ? (t "\n" A[i] "\n") : (A[i] "\n");
                      if(_debug)   { print(_qt_("_in_escape:", _in_escape, t, "end"));   fflush(); }
                      if($0 == "")   next;
              }
      }

      if(_in_escape)   {
              s = _esc1 " " $0 " " _esc2 " " _bs2;
              t = t ? (t "\n" s) : s;
              next;
      }              

      if($0 ~ /\\\\$/)   {
              s = $0;
      }   else   if($0 ~ /`$/)   {
              s = substr($0, 1, length($0) - 1);
      }   else       s = $0 "\\\\";

      gsub("\&", "\\&#38;", s);
      gsub("\t", "       ", s);
      while(gsub("   ", " \\&nbsp; ", s))   ;
#:       & Special in gsub, use "\\&"

      while(gsub("\&nbsp; \&nbsp;",               \
              "\\&nbsp;\\&nbsp;\\&nbsp;", s))   ;

      gsub("\\\ ", " ", s);
      gsub("{{&#123\\\\}}&#125", "\\", s);
      if($1 ~ "^\043")   sub("\043", "\\&#35;", s);   #:   the #

      if(s ~ /\\\\$/)   {
              s = substr(s, 1, length(s) - 2);
              gsub("\\\\", "\\&#92;", s);
              s = s "\134\134";   #:   the back slash
      }   else       gsub("\\\\", "\\&#92;", s);

      i = index(s, "[[");       j = index(s, "\|");       k = index(s, "]]");      
      if( 0 < i && i < j && j < k)   s = substr(s, 1, j - 1) "\377" substr(s, j + 1);
      gsub("\\\052\\\052", "\\&#42;\\&#42;", s);   #:   **
      gsub("\\\173\\\173\\\173", "\\&#123;\\&#123;\\&#123", s);   #:   {{&#123
      gsub("\\\175\\\175\\\175", "\\&#125;\\&#125;\\&#125", s);   #:   }}&#125
      gsub("\\\377", "\|", s);
      t = t ? (t "\n" s) : s;
}

END   {
      print(t);
      if(_clip)   _put2clip(t);
}

function       _put2clip(s,   c)   {
      c = (c ? c : (_u ? _u : "%u%")) "\\gclip.exe";
      printf("%s", s) | c;
      return(close(c));
}

function       _qt_(s, t, u, v,   ss)   {
      ss = "1: `" s;       ss = ss "`\t2: `" t;
      ss = ss "`\t3: `" u;       ss = ss "`\t4: `" v;
      return((ss "`"));
}
##   _____________________________________________

#:   Comments:       2012-09-09   15:46:51
#:   paradigm:   A, B.. arrays;   s..v strings;   i..n integers;   w..z reals
#: regular expression metacharacters: \ ^ $ . [ ] | ( ) * + ? escape with \
#: filenames may Not contain: \ / : * ? " < > |
#: cmd.exe special: & " ^ % < > | = ( ) @
#: start ["title"] [/min|/max] [/low|/high] [/wait] [/b] cmd
#: start /wait exefile & call echo.^%ERRORLEVEL^%
#: %u%nircmd.exe exec show|hide|min|max exefile opts
#: shexec open|print filename
#: \a \007 sp\040 !\041 "\042 #\043 $\044 %\045 &\046
#: '\047 *\052 +\053 -\055 .\056 /\057 <\074 =\075 >\076
#: ?\077 @\100 \\134 ^\136 `\140 |\174 ~\176 ' '\240
##   _____________________________________________
#:       EOF