summaryrefslogtreecommitdiff
path: root/utils/fpdoc/dw_basemd.pp
blob: 46de0ee3142b407ba4b1b914df651c230247cd21 (plain)
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
{
    FPDoc  -  Free Pascal Documentation Tool
    Copyright (C) 2021 by Michael Van Canneyt

    * Basic Markdown output generator. No assumptions about document/documentation structure

    See the file COPYING, included in this distribution,
    for details about the copyright.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}
unit dw_basemd;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, dwriter, DOM, pastree, dglobals;

Const
  MaxIndents = 10;
  MaxLists = 10;

Type
  THeaderLevel = 1..6;
  TRender = (rStrong,rEmphasis,rCode);
  TListType = (ltOrdered,ltUnordered, ltDefinition);

  TMarkdownEngine = (meMkDocs,meNone);

  { TBaseMarkdownWriter }

  TBaseMarkdownWriter = class(TMultifileDocWriter)
  private
    FIgnoreCount : Integer;
    FBaseImageURL: String;
    FContentPrefix: String;
    FCurrentIndentIndex: Word;
    FCurrentLine: UTF8String;
    FDefinitionSeparator: String;
    FDefinitionTermRender: TRender;
    FFileRendering: TRender;
    FIndentSize: Byte;
    FKeywordRendering: TRender;
    FPrefix : string;
    FMetadata,
    FMarkDown: TStrings;
    FSymbolRendering: TRender;
    FTheme: String;
    FUnderLineRendering: TRender;
    FVarRendering: TRender;
    FLink : String;
    FListStack : Integer;
    FIndents : Array[0..MaxIndents] of Word;
    FListTypes : Array[1..MaxLists] of TListType;
    FTableColCount : Integer;
    FMarkDownEngine: TMarkdownEngine;
    function GetCurrentIndent: Word;
    procedure SetIndentSize(AValue: Byte);
    procedure clearIndent;
    procedure CalcPrefix;
  Protected
    function CreateAllocator: TFileAllocator; override;
    Procedure DescrEmitNotesHeader(AContext : TPasElement); override;
    Procedure DescrEmitNotesFooter(AContext : TPasElement); override;
    procedure DescrWriteText(const AText: DOMString); override;
    procedure DescrBeginBold; override;
    procedure DescrEndBold; override;
    procedure DescrBeginItalic; override;
    procedure DescrEndItalic; override;
    procedure DescrBeginEmph; override;
    procedure DescrEndEmph; override;
    procedure DescrBeginUnderline; override;
    procedure DescrEndUnderline; override;
    procedure DescrWriteImageEl(const AFileName, ACaption, ALinkName : DOMString); override;
    procedure DescrWriteFileEl(const AText: DOMString); override;
    procedure DescrWriteKeywordEl(const AText: DOMString); override;
    procedure DescrWriteVarEl(const AText: DOMString); override;
    procedure DescrBeginLink(const AId: DOMString); override;
    procedure DescrEndLink; override;
    procedure DescrBeginURL(const AURL: DOMString); override;
    procedure DescrEndURL; override;
    procedure DescrWriteLinebreak; override;
    procedure DescrBeginParagraph; override;
    procedure DescrEndParagraph; override;
    procedure DescrBeginCode(HasBorder: Boolean; const AHighlighterName: String); override;
    procedure DescrWriteCodeLine(const ALine: String); override;
    procedure DescrEndCode; override;
    procedure DescrBeginOrderedList; override;
    procedure DescrEndOrderedList; override;
    procedure DescrBeginUnorderedList; override;
    procedure DescrEndUnorderedList; override;
    procedure DescrBeginDefinitionList; override;
    procedure DescrEndDefinitionList; override;
    procedure DescrBeginListItem; override;
    procedure DescrEndListItem; override;
    procedure DescrBeginDefinitionTerm; override;
    procedure DescrEndDefinitionTerm; override;
    procedure DescrBeginDefinitionEntry; override;
    procedure DescrEndDefinitionEntry; override;
    procedure DescrBeginSectionTitle; override;
    procedure DescrBeginSectionBody; override;
    procedure DescrEndSection; override;
    procedure DescrBeginRemark; override;
    procedure DescrEndRemark; override;
    procedure DescrBeginTable(ColCount: Integer; HasBorder: Boolean); override;
    procedure DescrEndTable; override;
    procedure DescrBeginTableCaption; override;
    procedure DescrEndTableCaption; override;
    procedure DescrBeginTableHeadRow; override;
    procedure DescrEndTableHeadRow; override;
    procedure DescrBeginTableRow; override;
    procedure DescrEndTableRow; override;
    procedure DescrBeginTableCell; override;
    procedure DescrEndTableCell; override;
  Protected
    // Emit current line, if any
    Function OutputCurrentLine : Boolean;
    function EscapeMarkDown(aText: Domstring): string;
    function EscapeMarkDown(aText: String): string;
    function CreateLink(Const aText,aLink : String) : String;
    function GetListPrefix: String;virtual;

    // Append to current line
    procedure AppendToLine(aText: DomString; DoEscape: boolean = true);
    procedure AppendToLine(aText: UTF8String; DoEscape: boolean = true); virtual;
    procedure AppendToLine(aFmt: UTF8String; aArgs : Array of const; DoEscape: boolean = true);
    // Write current line and append new line
    procedure EmitLine(aText: UTF8String; DoEscape: boolean = true); virtual;
    procedure EmitLine(aFmt: UTF8String; aArgs : Array of const; DoEscape: boolean = true);
    // Append spans to current line
    procedure AppendLink(Const aText,aLink : String);
    Procedure AppendRendered(aText : String; aRender : TRender);
    Procedure AppendKeyWord(aText : String); inline;
    Procedure AppendSymbol(aText : String); inline;
    Procedure AppendTableHeader(aHeaders: Array of String);
    Procedure EmitCode(aCodeBlock : String; aIndent : Integer = 0);
    Procedure EmitCode(aCodeBlock : TStrings; aIndent : Integer = 0);
    Procedure EmitCodeLine(aCodeLine : string);
    procedure EndSpan(aRender: TRender);
    procedure StartSpan(aRender: TRender);
    Procedure PushIndent(aNewIndent : Byte);
    Procedure PopIndent;
    Procedure StartList(aType : TListType);
    Procedure StopList(aType : TListType);
    Procedure BeginIgnore;
    Procedure EndIgnore;
    Procedure DoLineBreak;
    Function InList : Boolean;
    Function CurrentList : TListType;
    Property ContentPrefix : String Read FContentPrefix Write FContentPrefix;
  Public
    Constructor Create(APackage: TPasPackage; AEngine: TFPDocEngine); override;
    destructor Destroy;  override;
    Procedure AppendHeader(aLevel : THeaderLevel; const AHeader : String; DoEscape : Boolean = true);
    Procedure Indent;
    Procedure Undent;
    Procedure ClearMarkDown;
    Procedure EnsureEmptyLine;
    procedure SaveToFile(aFileName : string);
    procedure AddMetaData(Const aName,aValue : string);
    Property CurrentLine : UTF8String Read FCurrentLine Write FCurrentLine;
    Property MarkDown : TStrings Read FMarkDown;
    Property CurrentIndent : Word Read GetCurrentIndent;
    Property Prefix : String Read FPrefix;
    Property IndentSize : Byte Read FIndentSize Write SetIndentSize;
    Property UnderLineRendering : TRender Read FUnderLineRendering Write FUnderLineRendering;
    Property FileRendering : TRender Read FFileRendering Write FFileRendering;
    Property VarRendering : TRender Read FVarRendering Write FVarRendering;
    Property KeywordRendering : TRender Read FKeywordRendering Write FKeyWordRendering;
    Property SymbolRendering : TRender Read FSymbolRendering Write FSymbolRendering;
    Property DefinitionSeparator : String Read FDefinitionSeparator Write FDefinitionSeparator;
    Property DefinitionTermRender : TRender Read FDefinitionTermRender Write FDefinitionTermRender;
    Property BaseImageURL : String Read FBaseImageURL Write FBaseIMageURL;
    Property MetaData : TStrings Read FMetaData;
    Property MarkDownEngine : TMarkdownEngine Read FMarkDownEngine Write FMarkDownEngine;
    Property Theme : String Read FTheme Write FTheme;
  end;



implementation

resourcestring
  SErrCannotChangeIndentSizeWhenIndented = 'Cannot change indent size while text is indented.';
  SErrIndentMismatch = 'Indent mismatch: trying to undent when current indent too small';
  SErrNotInList = 'Not in list';
  SErrPopListStack = 'Pop list stack list type mismatch';
  SErrMinListStack = 'Min list stack reached';
  SErrMaxListStack = 'Max list stack reached';
  SErrMinIndentStack = 'Min indent stack reached';
  SErrMaxIndentStack = 'Max indent stack reached';

procedure TBaseMarkdownWriter.SetIndentSize(AValue: Byte);
begin
  if FIndentSize=AValue then Exit;
  if CurrentIndent>0 then
    FPDocError(SErrCannotChangeIndentSizeWhenIndented);
  FIndentSize:=AValue;
end;

function TBaseMarkdownWriter.GetCurrentIndent: Word;
begin
  Result:=FIndents[FCurrentIndentIndex];
end;

procedure TBaseMarkdownWriter.clearIndent;
begin
  FIndents[FCurrentIndentIndex]:=0;
  CalcPrefix;
end;

procedure TBaseMarkdownWriter.CalcPrefix;

begin
  FPrefix:=StringOfChar(' ',CurrentIndent);
end;

function TBaseMarkdownWriter.CreateAllocator: TFileAllocator;
begin
  Result:=TLongNameFileAllocator.Create('.md');
end;

procedure TBaseMarkdownWriter.DescrEmitNotesHeader(AContext: TPasElement);
begin
  AppendHeader(2, SDocNotes);
end;

procedure TBaseMarkdownWriter.DescrEmitNotesFooter(AContext: TPasElement);
begin
  EnsureEmptyLine;
end;

function TBaseMarkdownWriter.EscapeMarkDown(aText: Domstring): string;

begin
  Result:=EscapeMarkDown(UTF8Encode(aText))
end;

function TBaseMarkdownWriter.EscapeMarkDown(aText: String): string;
begin
  Result:=StringReplace(aText,'*','\*',[rfReplaceAll]);
  Result:=StringReplace(Result,'_','\_',[rfReplaceAll]);
  Result:=StringReplace(Result,'`','\`',[rfReplaceAll]);
end;

function TBaseMarkdownWriter.CreateLink(const aText, aLink: String): String;
begin
  Result:=Format('[%s](%s)',[EscapeMarkDown(aText),aLink])
end;

procedure TBaseMarkdownWriter.AppendToLine(aText: DomString; DoEscape: boolean);

begin
  If FIgnoreCount>0 then
    exit;
  AppendToLine(UTF8Encode(aText),DoEscape);
end;

procedure TBaseMarkdownWriter.AppendToLine(aText: UTF8String; DoEscape: boolean
  );
begin
  if DoEscape then
     aText:=EscapeMarkDown(aText);
  if (FCurrentLine='') and (FContentPrefix<>'') then
     FCurrentLine:=FContentPrefix;
  FCurrentLine:=FCurrentLine+aText;
end;

procedure TBaseMarkdownWriter.AppendToLine(aFmt: UTF8String;
  aArgs: array of const; DoEscape: boolean);
begin
  AppendToLine(Format(aFmt,aArgs),DoEscape);
end;

procedure TBaseMarkdownWriter.EmitLine(aText: UTF8String; DoEscape: boolean);
begin
  OutputCurrentLine;
  AppendToLine(aText,DoEscape);
  OutputCurrentLine;
end;

procedure TBaseMarkdownWriter.EmitLine(aFmt: UTF8String; aArgs: array of const;
  DoEscape: boolean);
begin
  EmitLine(Format(aFmt,aArgs),DoEscape);
end;

procedure TBaseMarkdownWriter.AppendLink(const aText, aLink: String);
begin
  AppendToLine(CreateLink(aText,aLink),False);
end;

procedure TBaseMarkdownWriter.AppendRendered(aText: String; aRender: TRender);
begin
  StartSpan(aRender);
  AppendToLine(aText);
  EndSpan(aRender);
end;

procedure TBaseMarkdownWriter.AppendKeyWord(aText: String);
begin
  AppendRendered(aText,KeywordRendering);
end;

procedure TBaseMarkdownWriter.AppendSymbol(aText: String);
begin
  AppendRendered(aText,SymbolRendering);
end;

procedure TBaseMarkdownWriter.AppendTableHeader(aHeaders: array of String);

Var
  S : String;

begin
  DescrBeginTable(Length(aHeaders),False);
  DescrBeginTableHeadRow;
  for S in aHeaders do
    begin
    DescrBeginTableCell;
    AppendToLine(S);
    DescrEndTableCell;
    end;
  DescrEndTableHeadRow;
end;


procedure TBaseMarkdownWriter.EmitCode(aCodeBlock: String; aIndent : Integer = 0);

Var
  L : TStringList;

begin
  L:=TStringList.Create;
  try
    L.Text:=aCodeBlock;
    EmitCode(L,aIndent);
  finally
    L.Free;
  end;
end;

procedure TBaseMarkdownWriter.EmitCode(aCodeBlock: TStrings; aIndent : Integer = 0);
var
  S,aPrefix : string;
begin
  aPrefix:=StringOfChar(' ',aIndent);
  For S in aCodeBlock do
    EmitCodeLine(aPrefix+S);
end;

procedure TBaseMarkdownWriter.EmitCodeLine(aCodeLine: string);
begin
  EmitLine(aCodeLine,False);
end;

procedure TBaseMarkdownWriter.DescrWriteText(const AText: DOMString);
begin
  AppendToLine(aText);
end;

procedure TBaseMarkdownWriter.DescrBeginBold;
begin
  AppendToLine('**',False);
end;

procedure TBaseMarkdownWriter.DescrEndBold;
begin
  AppendToLine('**',False);
end;

procedure TBaseMarkdownWriter.DescrBeginItalic;
begin
  AppendToLine('*',False);
end;

procedure TBaseMarkdownWriter.DescrEndItalic;
begin
  AppendToLine('*',False);
end;

procedure TBaseMarkdownWriter.DescrBeginEmph;
begin
  AppendToLine('*',False);
end;

procedure TBaseMarkdownWriter.DescrEndEmph;
begin
  AppendToLine('*',False);
end;

procedure TBaseMarkdownWriter.StartSpan(aRender : TRender);

begin
  case aRender of
    rStrong : AppendToLine('**',False);
    rEmphasis : AppendToLine('*',False);
    rCode : AppendToLine('`',False);
  end;
end;

procedure TBaseMarkdownWriter.PushIndent(aNewIndent: Byte);
begin
  if FCurrentIndentIndex>=MaxIndents then
     FPDocError(SErrMaxIndentStack);
  Inc(FCurrentIndentIndex);
  Findents[FCurrentIndentIndex]:=aNewIndent;
  CalcPrefix;
end;

procedure TBaseMarkdownWriter.PopIndent;
begin
  if FCurrentIndentIndex<=0 then
     FPDocError(SErrMinIndentStack);
  Dec(FCurrentIndentIndex);
  CalcPrefix;
end;

procedure TBaseMarkdownWriter.StartList(aType: TListType);
begin
  If FListStack>=MaxLists then
    FPDocError(SErrMaxListStack);
  OutputCurrentLine;
  Inc(FListStack);
  FListTypes[FListStack]:=aType;
  if FListStack>1 then
    PushIndent(CurrentIndent+Length(GetListprefix));
end;

procedure TBaseMarkdownWriter.StopList(aType: TListType);
begin
  OutputCurrentLine;
  If FListStack<=0 then
    FPDocError(SErrMinListStack);
  if FListTypes[FListStack]<>aType then
    FPDocError(SErrPopListStack);
  if FListStack>1 then
    PopIndent;
  Dec(FListStack);
end;

procedure TBaseMarkdownWriter.BeginIgnore;
begin
  Inc(FIgnoreCount);
end;

procedure TBaseMarkdownWriter.EndIgnore;
begin
  If FignoreCount>0 then
    Dec(FIgnoreCount);
end;

procedure TBaseMarkdownWriter.DoLineBreak;
begin
  if FCurrentLine<>'' then
    begin
    FCurrentLine:=FCurrentLine+'  ';
    OutputCurrentLine;
    end;
end;

function TBaseMarkdownWriter.InList: Boolean;
begin
  Result:=FlistStack>0;
end;

function TBaseMarkdownWriter.CurrentList: TListType;
begin
  if FListStack=0 then
    FPDOcError(SErrNotInList);
  Result:=FListTypes[FListStack];
end;

procedure TBaseMarkdownWriter.EndSpan(aRender : TRender);

begin
  case aRender of
    rStrong : AppendToLine('**',False);
    rEmphasis : AppendToLine('*',False);
    rCode : AppendToLine('`',False);
  end;
end;

procedure TBaseMarkdownWriter.DescrBeginUnderline;
begin
  StartSpan(UnderlineRendering);
end;

procedure TBaseMarkdownWriter.DescrEndUnderline;
begin
  EndSpan(UnderlineRendering);
end;

procedure TBaseMarkdownWriter.DescrWriteImageEl(const AFileName, ACaption, ALinkName : DOMString);

Var
  aLink,D,FN : String;
  L : integer;
begin
  // Determine URL for image.
  If (Module=Nil) then
    D:=Allocator.GetRelativePathToTop(Package)
  else
    D:=Allocator.GetRelativePathToTop(Module);
  L:=Length(D);
  If (L>0) and (D[L]<>'/') then
    D:=D+'/';

  FN:=D + BaseImageURL+ Utf8Encode(AFileName);
  EnsureEmptyLine;
  aLink:='!['+UTF8Encode(aCaption)+']('+FN+')';
  AppendToLine(aLink,False);
end;

procedure TBaseMarkdownWriter.DescrWriteFileEl(const AText: DOMString);

begin
  AppendRendered(UTF8Encode(aText),FileRendering);
end;

procedure TBaseMarkdownWriter.DescrWriteKeywordEl(const AText: DOMString);
begin
  AppendKeyWord(UTF8ENcode(aText));
end;

procedure TBaseMarkdownWriter.DescrWriteVarEl(const AText: DOMString);
begin
  AppendRendered(UTF8Encode(aText),VarRendering);
end;

procedure TBaseMarkdownWriter.DescrBeginLink(const AId: DOMString);
var
  a,s,n : String;

begin
  a:=UTF8Encode(AId);
  s := UTF8Encode(ResolveLinkID(a));

  if Length(s) > 0 then
    begin
    FLink:=S;
    AppendToLine('[');
    end
  else
    begin
    FLink:='';
    if assigned(module) then
      s:=module.name
    else
      s:='?';
    if a='' then a:='<empty>';
    if Assigned(CurrentContext) then
      N:=CurrentContext.Name
    else
      N:='?';
    DoLog(SErrUnknownLinkID, [s,n,a]);
    end
end;

procedure TBaseMarkdownWriter.DescrEndLink;
begin
  AppendToLine(']('+FLink+') ',false);
  FLink:='';
end;

procedure TBaseMarkdownWriter.DescrBeginURL(const AURL: DOMString);
begin
  FLink:=UTF8Encode(aURL);
  AppendToLine('[');
end;

procedure TBaseMarkdownWriter.DescrEndURL;
begin
  AppendToLine(']('+FLink+') ',false);
  FLink:='';
end;

procedure TBaseMarkdownWriter.DescrWriteLinebreak;
begin
  AppendToLine('  ');
  OutputCurrentLine;
end;

procedure TBaseMarkdownWriter.DescrBeginParagraph;
begin
  EnsureEmptyLine;
end;

procedure TBaseMarkdownWriter.DescrEndParagraph;
begin
  EnsureEmptyLine;
end;

procedure TBaseMarkdownWriter.DescrBeginCode(HasBorder: Boolean; const AHighlighterName: String);

Var
  hl : string;

begin
  hl:=AHighlighterName;
  if SameText(hl,'Pascal') or (hl='') then
    hl:='delphi';
  OutputCurrentLine;
  AppendToLine('```'+hl,False);
  PushIndent(0);
end;

procedure TBaseMarkdownWriter.DescrWriteCodeLine(const ALine: String);
begin
  EmitCodeLine(aLine);
end;

procedure TBaseMarkdownWriter.DescrEndCode;
begin
  OutputCurrentLine;
  AppendToLine('```',False);
  OutputCurrentLine;
  PopIndent;
end;

procedure TBaseMarkdownWriter.DescrBeginOrderedList;
begin
  StartList(ltOrdered);
end;

procedure TBaseMarkdownWriter.DescrEndOrderedList;
begin
  StopList(ltOrdered);
end;

procedure TBaseMarkdownWriter.DescrBeginUnorderedList;
begin
  StartList(ltUnordered);
end;

procedure TBaseMarkdownWriter.DescrEndUnorderedList;
begin
  StopList(ltUnordered);
end;

procedure TBaseMarkdownWriter.DescrBeginDefinitionList;
begin
  StartList(ltDefinition);
end;

procedure TBaseMarkdownWriter.DescrEndDefinitionList;
begin
  StopList(ltDefinition);
end;


function TBaseMarkdownWriter.GetListPrefix : String;

begin
  Case CurrentList of
    ltOrdered   : Result:='1. ';
    ltUnordered : Result:='- ';
    ltDefinition : Result:=':    ';
  end;
end;

procedure TBaseMarkdownWriter.DescrBeginListItem;
Var
  Pref : String;
begin
  Pref:=GetListPrefix;
  AppendToLine(Pref);
end;


procedure TBaseMarkdownWriter.DescrEndListItem;
begin
  OutputCurrentLine;
end;

procedure TBaseMarkdownWriter.DescrBeginDefinitionTerm;
begin
  if MarkDownEngine=meMkDocs then
    EnsureEmptyLine
  else
    begin
    AppendToLine('- ');
    StartSpan(DefinitionTermRender);
    end;
end;

procedure TBaseMarkdownWriter.DescrEndDefinitionTerm;
begin
  if MarkDownEngine=meMkDocs then
    OutputCurrentLine
  else
    EndSpan(DefinitionTermRender);
end;

procedure TBaseMarkdownWriter.DescrBeginDefinitionEntry;
begin
  if MarkDownEngine=meMkDocs then
    begin
    AppendToLine(':    ');
    Indent;
    end
  else
    AppendToLine(DefinitionSeparator);
end;

procedure TBaseMarkdownWriter.DescrEndDefinitionEntry;
begin
  OutputCurrentLine;
  if MarkDownEngine=meMkDocs then
    begin
    Undent;
    EnsureEmptyLine;
    end;
end;

procedure TBaseMarkdownWriter.DescrBeginSectionTitle;
begin
  EnsureEmptyLine;
end;

procedure TBaseMarkdownWriter.DescrBeginSectionBody;
begin
  EnsureEmptyLine;
end;

procedure TBaseMarkdownWriter.DescrEndSection;
begin
  EnsureEmptyLine;
end;

procedure TBaseMarkdownWriter.DescrBeginRemark;
begin
  if MarkDownEngine=meMkDocs then
    begin
    EnsureEmptyLine;
    AppendToLine('!!! Remark',False);
    OutputCurrentLine;
    end
  else
    FContentPrefix:='> ';
end;

procedure TBaseMarkdownWriter.DescrEndRemark;
begin
  if MarkDownEngine=meMkDocs then
    begin
    OutputCurrentLine;
    AppendToLine('!!!',False);
    end;
  EnsureEmptyLine;
  FContentPrefix:='';
end;

procedure TBaseMarkdownWriter.DescrBeginTable(ColCount: Integer; HasBorder: Boolean);
begin
  EnsureEmptyLine;
  FTableColCount:=ColCount;
end;

procedure TBaseMarkdownWriter.DescrEndTable;
begin
  EnsureEmptyLine;
end;

procedure TBaseMarkdownWriter.DescrBeginTableCaption;
begin
  BeginIgnore;
end;

procedure TBaseMarkdownWriter.DescrEndTableCaption;
begin
  EndIgnore;
end;

procedure TBaseMarkdownWriter.DescrBeginTableHeadRow;

begin
end;

procedure TBaseMarkdownWriter.DescrEndTableHeadRow;

Var
  I : Integer;

begin
  AppendToLine(' |',False);
  OutputCurrentLine;
  AppendToLine('|',False);
  For I:=1 to FTableColCount do
    AppendToLine('---|',False);
  OutputCurrentLine;
end;

procedure TBaseMarkdownWriter.DescrBeginTableRow;
begin
end;

procedure TBaseMarkdownWriter.DescrEndTableRow;
begin
  AppendToLine(' |',False);
  OutputCurrentLine;
end;

procedure TBaseMarkdownWriter.DescrBeginTableCell;
begin
  AppendToLine('| ',False);
end;

procedure TBaseMarkdownWriter.DescrEndTableCell;
begin
  AppendToLine(' ',False);
end;

function TBaseMarkdownWriter.OutputCurrentLine: Boolean;
begin
  If FIgnoreCount>0 then
    exit(False);
  Result:=FCurrentLine<>'';
  if Result then
    begin
    FMarkDown.Add(Prefix+FCurrentLine);
    FCurrentLine:='';
    end;
end;

constructor TBaseMarkdownWriter.Create(APackage: TPasPackage;
  AEngine: TFPDocEngine);
begin
  inherited Create(APackage, AEngine);
  FMarkDown:=TStringList.Create;
  FMetaData:=TStringList.Create;
  FMetaData.NameValueSeparator:=':';
  FIndents[FCurrentIndentIndex]:=0;
  CalcPrefix;
  Theme:='readthedocs';
end;

destructor TBaseMarkdownWriter.Destroy;
begin
  FreeAndNil(FMarkDown);
  inherited Destroy;
end;

procedure TBaseMarkdownWriter.AppendHeader(aLevel: THeaderLevel;
  const AHeader: String; DoEscape : Boolean = true);
begin
  EnsureEmptyLine;
  AppendToLine(StringOfChar('#',aLevel)+' ',False);
  AppendToLine(aHeader,DoEscape);
end;

procedure TBaseMarkdownWriter.Indent;
begin
  Inc(FIndents[FCurrentIndentIndex],IndentSize);
  CalcPrefix;
end;

procedure TBaseMarkdownWriter.Undent;
begin
  if IndentSize>CurrentIndent then
    FPDocError(SErrIndentMismatch);
  Dec(FIndents[FCurrentIndentIndex],IndentSize);
  CalcPrefix;
end;

procedure TBaseMarkdownWriter.ClearMarkDown;
begin
  FMarkDown.Clear;
  FMetaData.Clear;
  FCurrentLine:='';
  FContentPrefix:='';
  FCurrentIndentIndex:=0;;
  FIndents[FCurrentIndentIndex]:=0;
  CalcPrefix;
end;


procedure TBaseMarkdownWriter.EnsureEmptyLine;
begin
  if OutputCurrentLine then
    FMarkDown.Add('')
  else if (FmarkDown.Count>0) and (FMarkDown[FmarkDown.Count-1]<>'') then
    FMarkDown.Add('');
 end;

procedure TBaseMarkdownWriter.SaveToFile(aFileName: string);

Var
  Doc: TStrings;
  I : Integer;
  N,V : String;

begin
  Doc:=TStringList.Create;
  try
    if FMetadata.Count>0 then
      begin
      Doc.Add('---');
      For I:=0 to FMetadata.Count-1 do
        begin
        FMetaData.GetNameValue(I,N,V);
        Doc.Add(Lowercase(N)+': "'+V+'"');
        end;
      Doc.Add('---');
      end;
    Doc.AddStrings(FMarkDown);
    Doc.SaveToFile(aFileName);
  finally
    Doc.Free;
  end;
end;

procedure TBaseMarkdownWriter.AddMetaData(const aName, aValue: string);
begin
  FMetadata.Values[aName]:=aValue;
end;

end.