Fossil

Diff
Login

Differences From Artifact [be3d69eb4f]:

To Artifact [dea3c5000f]:


434
435
436
437
438
439
440


441


















442
443
444
445
446
447
448
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







+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







#define TOKEN_NEWLINE       5  /* A single "\n" */
#define TOKEN_BUL_LI        6  /*  "  *  " */
#define TOKEN_NUM_LI        7  /*  "  #  " */
#define TOKEN_ENUM          8  /*  "  \(?\d+[.)]?  " */
#define TOKEN_INDENT        9  /*  "   " */
#define TOKEN_RAW           10 /* Output exactly (used when wiki-use-html==1) */
#define TOKEN_AUTOLINK      11 /* <URL> */
#define TOKEN_MDSPAN        12 /* Markdown span characters: * _ ` */
#define TOKEN_BACKSLASH     13 /* A backslash-escape */
#define TOKEN_TEXT          12 /* None of the above */
#define TOKEN_TEXT          14 /* None of the above */

static const char *wiki_token_names[] = { "",
  "MARKUP",
  "CHARACTER",
  "LINK",
  "PARAGRAPH",
  "NEWLINE",
  "BUL_LI",
  "NUM_LI",
  "ENUM",
  "INDENT",
  "RAW",
  "AUTOLINK",
  "MDSPAN",
  "BACKSLASH",
  "TEXT",
};

/*
** State flags.  Save the lower 16 bits for the WIKI_* flags.
*/
#define AT_NEWLINE          0x0010000  /* At start of a line */
#define AT_PARAGRAPH        0x0020000  /* At start of a paragraph */
#define ALLOW_WIKI          0x0040000  /* Allow wiki markup */
543
544
545
546
547
548
549

550
551
552


553
554
555
556



557


558
559

560
561
562
563
564
565
566
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







+



+
+




+
+
+
-
+
+

-
+







**
** Interesting characters are:
**
**      <
**      &
**      \n
**      [
**      _ * ` \    <-- WIKI_MARKDOWN_SPAN only.
**
** The "[" is only considered if flags contain ALLOW_LINKS or ALLOW_WIKI.
** The "\n" is only considered interesting if the flags constains ALLOW_WIKI.
** The markdown span characters, _ * ` and \, are only considered if both
** ALLOW_WIKI and WIKI_MARKDOWN_SPAN are set.
*/
static int textLength(const char *z, int flags){
  const char *zReject;
  if( flags & ALLOW_WIKI ){
    if( flags & WIKI_MARKDOWN_SPAN ){
      zReject = "_*`\\\n[<&";
    }else{
    zReject = "<&[\n";
      zReject = "\n[<&";
    }
  }else if( flags & ALLOW_LINKS ){
    zReject = "<&[";
    zReject = "[<&";
  }else{
    zReject = "<&";
  }
  return strcspn(z, zReject);
}

/*
673
674
675
676
677
678
679






680
681
682
683
684
685
686
687
688
689


690
691
692
693
694
695
696
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







+
+
+
+
+
+










+
+







}

/*
** Get the next wiki token.
**
** z points to the start of a token.  Return the number of
** characters in that token.  Write the token type into *pTokenType.
**
** Only wiki-style [target] links are recognized by this routine.
** For markdown-style [display](target) links, though routine only
** see the "[display]" part.  But the caller will recognize that
** "(target)" follows immediately afterwards and deal with that, if
** markdown-style hyperlinks are enabled.
*/
static int nextWikiToken(const char *z, Renderer *p, int *pTokenType){
  int n;
  if( z[0]=='<' ){
    n = html_tag_length(z);
    if( n>0 ){
      *pTokenType = TOKEN_MARKUP;
      return n;
    }
    if( z[1]=='h'
     && !p->inVerbatim
     && (p->state & (ALLOW_WIKI|ALLOW_LINKS))==(ALLOW_WIKI|ALLOW_LINKS)
     && (strncmp(z,"<https://",9)==0 || strncmp(z,"<http://",8)==0)
    ){
      for(n=8; z[n] && z[n]!='>'; n++){}
      if( z[n]=='>' ){
        *pTokenType = TOKEN_AUTOLINK;
        return n+1;
      }
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
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







+
+
+
+
+
+
+
+
+
+
+
+














+
+
+
+
+
+
+
+
+







        *pTokenType = TOKEN_INDENT;
        return n;
      }
    }
    if( z[0]=='[' && (n = linkLength(z))>0 ){
      *pTokenType = TOKEN_LINK;
      return n;
    }
    if( z[0]=='*' || z[0]=='_' || z[0]=='`' ){
      *pTokenType = TOKEN_MDSPAN;
      return 1 + (z[1]==z[0]);
    }
    if( z[0]=='\\' ){
      if( z[1]==0 || fossil_isspace(z[1]) || (z[1]&0x80)!=0 ){
        *pTokenType = TOKEN_TEXT;
        return 1;
      }
      *pTokenType = TOKEN_BACKSLASH;
      return 2;
    }
  }else if( (p->state & ALLOW_LINKS)!=0 && z[0]=='[' && (n = linkLength(z))>0 ){
    *pTokenType = TOKEN_LINK;
    return n;
  }
  *pTokenType = TOKEN_TEXT;
  return 1 + textLength(z+1, p->state);
}

/*
** Parse only Wiki links, return everything else as TOKEN_RAW.
**
** z points to the start of a token.  Return the number of
** characters in that token. Write the token type into *pTokenType.
**
** Only wiki-style [target] links are recognized by this routine.
** For markdown-style [display](target) links, though routine only
** see the "[display]" part.  But the caller will recognize that
** "(target)" follows immediately afterwards and deal with that, if
** markdown-style hyperlinks are enabled.
**
** Auto-links ("<URL>") are not recognized at all, since they are
** not back-referenced.
*/
static int nextRawToken(const char *z, Renderer *p, int *pTokenType){
  int n;
  if( z[0]=='[' && (n = linkLength(z))>0 ){
    *pTokenType = TOKEN_LINK;
    return n;
  }
1642
1643
1644
1645
1646
1647
1648











1649
1650
1651
1652
1653
1654
1655
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721







+
+
+
+
+
+
+
+
+
+
+







          p->state |= FONT_MARKUP_ONLY;
          wiki_render(p, zDisplay);
          p->state = savedState;
          blob_append(p->pOut, zClose, -1);
        }
        break;
      }
      case TOKEN_BACKSLASH: {
        if( (p->state & WIKI_MARKDOWN_SPAN)==0 ){
          /* Ignore backslashes in traditional Wiki */
          blob_append_char(p->pOut, '\\');
          n = 1;
        }else{
          blob_append_char(p->pOut, z[1]);
        }
        break;
      }
      case TOKEN_MDSPAN:
      case TOKEN_TEXT: {
        int i;
        for(i=0; i<n && fossil_isspace(z[i]); i++){}
        if( i<n ) startAutoParagraph(p);
        blob_append(p->pOut, z, n);
        break;
      }
1904
1905
1906
1907
1908
1909
1910


































1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929

1930
1931
1932
1933
1934

1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945

1946
1947
1948
1949
1950



1951
1952
1953
1954
1955
1956
1957








1958
1959
1960
1961
1962
1963
1964
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034

2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055







2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+



















+




-
+











+





+
+
+
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+







  endAutoParagraph(&renderer);
  while( renderer.nStack ){
    popStack(&renderer);
  }
  blob_append_char(renderer.pOut, '\n');
  free(renderer.aStack);
}

/*
** Output a tokenization of the input file.  Debugging use only.
*/
static void test_tokenize(Blob *pIn, Blob *pOut, int flags){
  Renderer renderer;
  int tokenType;
  int n;
  int wikiHtmlOnly = (flags & (WIKI_HTMLONLY | WIKI_LINKSONLY))!=0;
  char *z = blob_str(pIn);

  /* Make sure the attribute constants and names still align
  ** following changes in the attribute list. */
  assert( fossil_strcmp(aAttribute[ATTR_WIDTH].zName, "width")==0 );

  memset(&renderer, 0, sizeof(renderer));
  renderer.renderFlags = flags;
  renderer.state = ALLOW_WIKI|flags;
  while( z[0] ){
    char cSave;
    if( wikiHtmlOnly ){
      n = nextRawToken(z, &renderer, &tokenType);
    }else{
      n = nextWikiToken(z, &renderer, &tokenType);
    }
    cSave = z[n];
    z[n] = 0;
    blob_appendf(pOut, "%-12s %z\n",
       wiki_token_names[tokenType],
       encode_json_string_literal(z, 1, 0));
    z[n] = cSave;
    z += n;
  }
}

/*
** COMMAND: test-wiki-render
**
** Usage: %fossil test-wiki-render FILE [OPTIONS]
**
** Translate the input FILE from Fossil-wiki into HTML and write
** the resulting HTML on standard output.
**
** Options:
**    --buttons        Set the WIKI_BUTTONS flag
**    --dark-pikchr    Render pikchrs in dark mode
**    --htmlonly       Set the WIKI_HTMLONLY flag
**    --inline         Set the WIKI_INLINE flag
**    --linksonly      Set the WIKI_LINKSONLY flag
**    --md-span        Allow markdown span syntax: links and emphasis marks
**    --nobadlinks     Set the WIKI_NOBADLINKS flag
**    --noblock        Set the WIKI_NOBLOCK flag
**    --text           Run the output through html_to_plaintext().
**    --tokenize       Output a tokenization of the input file
*/
void test_wiki_render(void){
  Blob in, out;
  int flags = 0;
  int bText;
  int bText, bTokenize;
  if( find_option("buttons",0,0)!=0 ) flags |= WIKI_BUTTONS;
  if( find_option("htmlonly",0,0)!=0 ) flags |= WIKI_HTMLONLY;
  if( find_option("linksonly",0,0)!=0 ) flags |= WIKI_LINKSONLY;
  if( find_option("nobadlinks",0,0)!=0 ) flags |= WIKI_NOBADLINKS;
  if( find_option("inline",0,0)!=0 ) flags |= WIKI_INLINE;
  if( find_option("noblock",0,0)!=0 ) flags |= WIKI_NOBLOCK;
  if( find_option("md-span",0,0)!=0 ) flags |= WIKI_MARKDOWN_SPAN;
  if( find_option("dark-pikchr",0,0)!=0 ){
    pikchr_to_html_add_flags( PIKCHR_PROCESS_DARK_MODE );
  }
  bText = find_option("text",0,0)!=0;
  bTokenize = find_option("tokenize",0,0)!=0;
  db_find_and_open_repository(OPEN_OK_NOT_FOUND|OPEN_SUBSTITUTE,0);
  verify_all_options();
  if( g.argc!=3 ) usage("FILE");
  blob_zero(&out);
  blob_read_from_file(&in, g.argv[2], ExtFILE);
  if( bTokenize ){
    test_tokenize(&in, &out, flags);
  }else{
  wiki_convert(&in, &out, flags);
  if( bText ){
    Blob txt;
    blob_init(&txt, 0, 0);
    html_to_plaintext(blob_str(&out),&txt);
    blob_reset(&out);
    out = txt;
    wiki_convert(&in, &out, flags);
    if( bText ){
      Blob txt;
      blob_init(&txt, 0, 0);
      html_to_plaintext(blob_str(&out),&txt);
      blob_reset(&out);
      out = txt;
    }
  }
  blob_write_to_file(&out, "-");
}

/*
** COMMAND: test-markdown-render
**