Diff
Not logged in

Differences From Artifact [46240c2954]:

To Artifact [e9652834e8]:


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
** structure, in its own separate memory allocation.
*/
typedef struct MarkdownHeading MarkdownHeading;
struct MarkdownHeading {
  MarkdownHeading *pPrev, *pNext;  /* List of them all */
  char *zTitle;                    /* Text as displayed */
  char *zTag;                      /* Pandoc-style tag */


};

/*
** An instance of the following structure is passed through the
** "opaque" pointer.
*/
typedef struct MarkdownToHtml MarkdownToHtml;
struct MarkdownToHtml {
  Blob *output_title;                /* Store the title here */
  MarkdownHeading *pFirst, *pList;   /* List of all headings */
  int iToc;         /* Where to insert table-of-contents */
  int mxToc;        /* Maximum table-of-content level */

  int iHdngNums;    /* True to automatically number headings */
  int aNum[6];      /* Most recent number at each level */
};































































































/* INTER_BLOCK -- skip a line between block level elements */
#define INTER_BLOCK(ob) \
  do { if( blob_size(ob)>0 ) blob_append_char(ob, '\n'); } while (0)

/* BLOB_APPEND_LITERAL -- append a string literal to a blob */
#define BLOB_APPEND_LITERAL(blob, literal) \







>
>









|


>




>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
** structure, in its own separate memory allocation.
*/
typedef struct MarkdownHeading MarkdownHeading;
struct MarkdownHeading {
  MarkdownHeading *pPrev, *pNext;  /* List of them all */
  char *zTitle;                    /* Text as displayed */
  char *zTag;                      /* Pandoc-style tag */
  int iLevel;                      /* Level number for this entry */
  int nth;                         /* This is the nth with the same tag */
};

/*
** An instance of the following structure is passed through the
** "opaque" pointer.
*/
typedef struct MarkdownToHtml MarkdownToHtml;
struct MarkdownToHtml {
  Blob *output_title;                /* Store the title here */
  MarkdownHeading *pFirst, *pLast;   /* List of all headings */
  int iToc;         /* Where to insert table-of-contents */
  int mxToc;        /* Maximum table-of-content level */
  int mnLevel;      /* Minimum level seen over all headings */
  int iHdngNums;    /* True to automatically number headings */
  int aNum[6];      /* Most recent number at each level */
};

/*
** Add a new heading to the heading list.  This involves generating
** a Pandoc-compatible identifier based on the heading text.
*/
static void html_new_heading(MarkdownToHtml *pCtx, Blob *text, int iLevel){
  MarkdownHeading *pNew, *pSearch;
  int nText = blob_size(text);
  size_t n = sizeof(*pNew) + nText*2 + 10;
  const char *zText = blob_buffer(text);
  char *zTag;
  int i, j;
  int seenChar = 0;

  pNew = fossil_malloc( n );
  memset(pNew, 0, n);
  if( pCtx->pLast ){
    pCtx->pLast->pNext = pNew;
    if( pCtx->mnLevel>iLevel ) pCtx->mnLevel = iLevel;
  }else{
    pCtx->mnLevel = iLevel;
  }
  pNew->pPrev = pCtx->pLast;
  pCtx->pLast = pNew;
  if( pCtx->pFirst==0 ) pCtx->pFirst = pNew;
  pNew->zTitle = (char*)&pNew[1];
  memcpy(pNew->zTitle, zText, nText);
  pNew->zTitle[nText] = 0;
  pNew->zTag = pNew->zTitle + nText + 1;
  pNew->iLevel = iLevel;
  pNew->nth = 0;

  /* Generate an identifier.  The identifer name is approximately the
  ** same as a Pandoc identifier.
  **
  **  *  Skip all text up to the first letter.
  **  *  Remove all text past the last letter.
  **  *  Remove HTML markup and entities.
  **  *  Replace all whitespace sequences with a single "-"
  **  *  Remove all characters other than alphanumeric, "_", "-", and ".".
  **  *  Convert all alphabetics to lower case.
  **  *  If nothing remains, use "section" as the identifier.
  */
  while( nText>0 && !fossil_isalpha(zText[nText-1]) ){ nText--; }
  memcpy(pNew->zTag, zText, nText);
  pNew->zTag[nText] = 0;
  zTag = pNew->zTag;
  for(i=j=0; zTag[i]; i++){
    if( fossil_isupper(zTag[i]) ){
      if( !seenChar ){ j = 0; seenChar = 1; }
      zTag[j++] = fossil_tolower(zTag[i]);
      continue;
    }
    if( fossil_islower(zTag[i]) ){
      if( !seenChar ){ j = 0; seenChar = 1; }
      zTag[j++] = zTag[i];
      continue;
    }
    if( zTag[i]=='<' ){
      i += html_tag_length(zTag+i) - 1;
      continue;
    }
    if( zTag[i]=='&' ){
      while( zTag[i] && zTag[i]!=';' ){ i++; }
      if( zTag[i]==0 ) break;
      continue;
    }
    if( fossil_isspace(zTag[i]) ){
      zTag[j++] = '-';
      while( fossil_isspace(zTag[i+1]) ){ i++; }
      continue;
    }
    if( !fossil_isalnum(zTag[i]) && zTag[i]!='.' && zTag[i]!='_' ){
      zTag[j++] = '-';
    }else{
      zTag[j++] = zTag[i];
    }
  }
  if( j==0 || !seenChar ){
    memcpy(zTag, "section", 7);
    j = 7;
  }
  while( j>0 && !fossil_isalpha(zTag[j-1]) ){ j--; }
  zTag[j] = 0;

  /* Search for duplicate identifiers and disambiguate */
  pNew->nth = 0;
  for(pSearch=pNew->pPrev; pSearch; pSearch=pSearch->pPrev){
    if( strcmp(pSearch->zTag,zTag)==0 ){
      pNew->nth = pSearch->nth+1;
    }
  }
}   


/* INTER_BLOCK -- skip a line between block level elements */
#define INTER_BLOCK(ob) \
  do { if( blob_size(ob)>0 ) blob_append_char(ob, '\n'); } while (0)

/* BLOB_APPEND_LITERAL -- append a string literal to a blob */
#define BLOB_APPEND_LITERAL(blob, literal) \
244
245
246
247
248
249
250

251
252
253
254
255
256
257
258





259

260
261
262
263
264
265
266
static void html_header(
  struct Blob *ob,
  struct Blob *text,
  int level,
  void *opaque
){
  MarkdownToHtml *pCtx = (MarkdownToHtml*)opaque;

  struct Blob *title = pCtx->output_title;
  /* The first header at the beginning of a text is considered as
   * a title and not output. */
  if( blob_size(ob)<=PROLOG_SIZE && title!=0 && blob_size(title)==0 ){
    BLOB_APPEND_BLOB(title, text);
    return;
  }
  INTER_BLOCK(ob);





  blob_appendf(ob, "<h%d>", level);

  if( pCtx->iHdngNums && level>=pCtx->iHdngNums ){
    int i;
    for(i=pCtx->iHdngNums-1; i<level-1; i++){
      blob_appendf(ob,"%d.",pCtx->aNum[i]);
    }
    blob_appendf(ob,"%d", ++pCtx->aNum[i]);
    if( i==pCtx->iHdngNums-1 ) blob_append(ob, ".0", 2);







>








>
>
>
>
>
|
>







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
static void html_header(
  struct Blob *ob,
  struct Blob *text,
  int level,
  void *opaque
){
  MarkdownToHtml *pCtx = (MarkdownToHtml*)opaque;
  MarkdownHeading *pHdng;
  struct Blob *title = pCtx->output_title;
  /* The first header at the beginning of a text is considered as
   * a title and not output. */
  if( blob_size(ob)<=PROLOG_SIZE && title!=0 && blob_size(title)==0 ){
    BLOB_APPEND_BLOB(title, text);
    return;
  }
  INTER_BLOCK(ob);
  html_new_heading(pCtx, text, level);
  pHdng = pCtx->pLast;
  if( pHdng->nth ){
    blob_appendf(ob, "<h%d id='%h-%d'>", level, pHdng->zTag, pHdng->nth);
  }else{
    blob_appendf(ob, "<h%d id='%h'>", level, pHdng->zTag);
  }
  if( pCtx->iHdngNums && level>=pCtx->iHdngNums ){
    int i;
    for(i=pCtx->iHdngNums-1; i<level-1; i++){
      blob_appendf(ob,"%d.",pCtx->aNum[i]);
    }
    blob_appendf(ob,"%d", ++pCtx->aNum[i]);
    if( i==pCtx->iHdngNums-1 ) blob_append(ob, ".0", 2);
605
606
607
608
609
610
611











































612
613
614
615
616
617
618
  return 1;
}


static void html_normal_text(struct Blob *ob, struct Blob *text, void *opaque){
  html_escape(ob, blob_buffer(text), blob_size(text));
}












































/*
** Convert markdown into HTML.
**
** The document title is placed in output_title if not NULL.  Or if
** output_title is NULL, the document title appears in the body.
*/







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
  return 1;
}


static void html_normal_text(struct Blob *ob, struct Blob *text, void *opaque){
  html_escape(ob, blob_buffer(text), blob_size(text));
}

/*
** Insert a table of contents into the body of the document.
**
** The pCtx provides the information needed to do this:
**
**    pCtx->iToc              Offset into pOut of where to insert the TOC
**    pCtx->mxToc             Maximum depth of the TOC
**    pCtx->pFirst            List of paragraphs to form the TOC
*/
static void html_insert_toc(MarkdownToHtml *pCtx, Blob *pOut){
  Blob new;
  MarkdownHeading *pX;
  int iLevel = pCtx->mnLevel-1;
  int iBase = iLevel;
  blob_init(&new, 0, 0);
  blob_append(&new, blob_buffer(pOut), pCtx->iToc);
  blob_append(&new, "<div class='markdown-toc'>\n", -1);
  for(pX=pCtx->pFirst; pX; pX=pX->pNext){
    if( pX->iLevel>pCtx->mxToc ) continue;
    while( iLevel<pX->iLevel ){
      iLevel++;
      blob_appendf(&new, "<ul class='markdown-toc%d markdown-toc'>\n",
                         iLevel - iBase);
    }
    while( iLevel>pX->iLevel ){
      iLevel--;
      blob_appendf(&new, "</ul>\n");
    }
    blob_appendf(&new,"<li><a href='#%h'>", pX->zTag);
    html_to_plaintext(pX->zTitle, &new);
    blob_appendf(&new,"</a></li>\n");
  }
  while( iLevel>iBase ){
    iLevel--;
    blob_appendf(&new, "</ul>\n");
  }
  blob_appendf(&new, "</div>\n");
  blob_append(&new, blob_buffer(pOut)+pCtx->iToc,
                    blob_size(pOut)-pCtx->iToc);
  blob_reset(pOut);
  *pOut = new;
}

/*
** Convert markdown into HTML.
**
** The document title is placed in output_title if not NULL.  Or if
** output_title is NULL, the document title appears in the body.
*/
663
664
665
666
667
668
669

670
671
672
673
674

  memset(&context, 0, sizeof(context));
  context.output_title = output_title;
  html_renderer.opaque = &context;
  if( output_title ) blob_reset(output_title);
  blob_reset(output_body);
  markdown(output_body, input_markdown, &html_renderer);

  for(pHdng=context.pFirst; pHdng; pHdng=pNextHdng){
    pNextHdng = pHdng->pNext;
    fossil_free(pHdng);
  }
}







>





809
810
811
812
813
814
815
816
817
818
819
820
821

  memset(&context, 0, sizeof(context));
  context.output_title = output_title;
  html_renderer.opaque = &context;
  if( output_title ) blob_reset(output_title);
  blob_reset(output_body);
  markdown(output_body, input_markdown, &html_renderer);
  if( context.mxToc>0 ) html_insert_toc(&context, output_body);
  for(pHdng=context.pFirst; pHdng; pHdng=pNextHdng){
    pNextHdng = pHdng->pNext;
    fossil_free(pHdng);
  }
}