-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
edoc_html_to_markdown.erl
1211 lines (1139 loc) · 50 KB
/
edoc_html_to_markdown.erl
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
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%% -*- erlang -*-
%% %CopyrightBegin%
%%
%% Copyright Ericsson AB 2020-2024. All Rights Reserved.
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% %CopyrightEnd%
%% @doc This module can convert application/html+erlang style documentation to markdown.
%% Does translation of Erlang XML docs to EEP-48 doc chunks and
%% EEP-48 doc chunks to ex_doc markdown.
%%----------------------------------------------------------------------
-module(edoc_html_to_markdown).
-feature(maybe_expr, enable).
-include_lib("kernel/include/eep48.hrl").
-export([convert_html/2, convert_xml/2, convert_html/3, convert_xml/3]).
%% @doc
%% Convert `application/html+erlang' ({@type shell_docs:chunk_element()}) to Markdown
%% suitable for usage with ExDoc.
%%
%% The `Application' and `Module' argument are used to correctly generate links.
-spec convert_html(Application :: atom(),
Module :: module(),
Html :: shell_docs:chunk_elements()) ->
unicode:chardata().
convert_html(Application, Module, Html) when is_atom(Module), is_atom(Application) ->
put(module, Module),
put(application, atom_to_binary(Application)),
render_docs(shell_docs:normalize(Html)).
%% @hidden
convert_html(Application, Html) when is_atom(Application) ->
put(module, ''),
put(application, atom_to_binary(Application)),
render_docs(shell_docs:normalize(Html)).
%% @hidden
convert_xml(Application, Binary) when is_atom(Application) ->
convert_xml(Application, '', Binary).
%% @hidden
convert_xml(Application, Module, Binary) when is_atom(Application), is_atom(Module) ->
put(application, atom_to_binary(Application)),
case xmerl_sax_parser:stream(iolist_to_binary(["<section>",Binary,"</section>"]),
[{event_fun, fun event/3},
{event_state, initial_state()}]) of
{ok, Tree, _} ->
convert_html(Application, Module, transform(get_dom(Tree), []))
end.
%%
%% The code below is taken from shell_docs and modified to emit Markdown instead
%%
-record(config, {docs, current = undefined }).
-define(ALL_ELEMENTS, [
a,
p,
'div',
br,
h1,
h2,
h3,
h4,
h5,
h6,
hr,
i,
b,
em,
strong,
pre,
code,
ul,
ol,
li,
dl,
dt,
dd,
table,
tr,
td
]).
%% inline elements are:
-define(INLINE, [i, b, em, strong, code, a]).
-define(IS_INLINE(ELEM),
(((ELEM) =:= a) orelse ((ELEM) =:= code) orelse
((ELEM) =:= i) orelse ((ELEM) =:= em) orelse
((ELEM) =:= b) orelse ((ELEM) =:= strong))
).
%% non-inline elements are:
-define(BLOCK, [p, 'div', pre, br, ul, ol, li, dl, dt, dd, h1, h2, h3, h4, h5, h6, hr, table, tr, td]).
-define(IS_BLOCK(ELEM), not ?IS_INLINE(ELEM)).
-define(IS_PRE(ELEM), ((ELEM) =:= pre)).
-define(IS_HDR(ELEM),
(((ELEM) =:= h1) orelse ((ELEM) =:= h2) orelse
((ELEM) =:= h3) orelse ((ELEM) =:= h4) orelse
((ELEM) =:= h5) orelse ((ELEM) =:= h6))).
%%% General rendering functions
render_docs(DocContents) ->
render_docs(DocContents, init_config(#docs_v1{ docs = [] }, #{})).
render_docs(DocContents, #config{} = Config) ->
render_docs(DocContents, 0, Config).
render_docs(DocContents, Ind, D = #config{}) when is_integer(Ind) ->
try
{Doc, _} = trimnl(render_docs(preprocess_docs(DocContents, D), [], 0, Ind, D)),
unicode:characters_to_binary(Doc)
catch throw:R:ST ->
io:format("Failed to render: ~tp~n",[R]),
erlang:raise(throw,R,ST);
E:R:ST ->
io:format("Failed to render: ~tp~n",[DocContents]),
erlang:raise(E,R,ST)
end.
%% Merge any anchor with its header
%% preprocess_docs([{Hdr,Attr,C},{a,[{id,_}] = Id,[]}|T], D) when ?IS_HDR(Hdr) ->
%% preprocess_docs([{Hdr,Attr ++ Id, C} | T], D);
%% preprocess_docs([{a,[{id,_}] = Id,[]},{Hdr,Attr,C}|T], D) when ?IS_HDR(Hdr) ->
%% preprocess_docs([{Hdr,Attr ++ Id, C} | T], D);
preprocess_docs([{a,[{id,_Id}|_] = AAttr,[]},{Tag,PAttr,C}|T], D)
when Tag =:= pre; Tag =:= em; Tag =:= table; Tag =:= code; Tag =:= img ->
preprocess_docs([{Tag, AAttr ++ PAttr, C}|T], D);
preprocess_docs([{Tag,_,_} = H,{a,[{id,_Id}|_],[]} = A|T],D) when ?IS_HDR(Tag) ->
preprocess_docs([A, H | T], D);
preprocess_docs([{a,[{id,Id}|_],[]} = A,{Tag,_,Name} = H|T],D) when ?IS_HDR(Tag) ->
case string:equal(render_elink_anchor(Id), render_elink_anchor(Name)) of
true ->
preprocess_docs([H|T], D);
false ->
[A | preprocess_docs([H|T], D)]
end;
preprocess_docs([{a,[{id,Id}] = Attr,[]}| T],
#config{ current = {{function,Function,Arity},_,_,_,_} } = D) ->
maybe
%% Remove any anchor that is just function-arity
[FunctionString, ArityString] ?= string:split(Id,"-",all),
Arity ?= catch binary_to_integer(ArityString),
true ?= is_integer(Arity),
Function ?= binary_to_atom(FunctionString),
preprocess_docs(T, D)
else
_ ->
[{a, Attr, []} | preprocess_docs(T, D)]
end;
preprocess_docs([{Tag,Attr,C}|T], D) ->
[{Tag,proplists:delete(ghlink,Attr),preprocess_docs(C, D)}|preprocess_docs(T, D)];
preprocess_docs([Text|T], D) when is_binary(Text) ->
[Text | preprocess_docs(T,D)];
preprocess_docs([], _) ->
[].
-spec init_config(#docs_v1{} | undefined, _) -> #config{}.
init_config(D, Config) ->
#config{docs = D, current = maps:get(current, Config, undefined) }.
render_docs(Elems, State, Pos, Ind, D) when is_list(Elems) ->
lists:mapfoldl(
fun(Elem, P) ->
render_docs(Elem, State, P, Ind, D)
end,
Pos,
Elems
);
render_docs(Elem, State, Pos, Ind, D) ->
% io:format("Elem: ~p (~p) (~p,~p)~n",[Elem,State,Pos,Ind]),
render_element(Elem, State, Pos, Ind, D).
%%% The function is the main element rendering function
%%%
%%% Elem: The current element to process
%%% Stack: A stack of element names to see where we are in the dom
%%% Pos: The current print position on the current line
%%% Ind: How much the text should be indented after a newline
%%% Config: The renderer's configuration
%%%
%%% Each element is responsible for putting new lines AFTER itself
%%% The indents are done either by render_words when a newline happens
%%% or when a new element is to be rendered and Pos < Ind.
%%%
%%% Any block elements (i.e. p, ul, li etc) are responsible for trimming
%%% extra new lines. eg. <ul><li><p>content</p></li></ul> should only
%%% have two newlines at the end.
%% render_element({IgnoreMe,_,Content}, State, Pos, Ind,D)
%% when IgnoreMe =:= a ->
%% render_docs(Content, State, Pos, Ind,D);
%% Catch h* before the padding is done as they reset padding
render_element({Tag = h1, Attr, Content}, State, 0 = Pos, _Ind, D) ->
{Docs, NewPos} = render_docs(Content, [Tag|State], Pos, 0, D),
trimnlnl({["# ", Docs, ial(Attr)], NewPos});
render_element({Tag = h2, Attr, Content}, State, 0 = Pos, _Ind, D) ->
{Docs, NewPos} = render_docs(Content, [Tag|State], Pos, 0, D),
trimnlnl({["## ", Docs, ial(Attr)], NewPos});
render_element({Tag = h3, Attr, Content}, State, Pos, _Ind, D) when Pos =< 2 ->
{Docs, NewPos} = render_docs(Content, [Tag|State], Pos, 0, D),
trimnlnl({["### ", Docs, ial(Attr)], NewPos});
render_element({Tag = h4, Attr, Content}, State, Pos, _Ind, D) when Pos =< 2 ->
{Docs, NewPos} = render_docs(Content, [Tag|State], Pos, 0, D),
trimnlnl({["#### ", Docs, ial(Attr)], NewPos});
render_element({Tag = h5, Attr, Content}, State, Pos, _Ind, D) when Pos =< 2 ->
{Docs, NewPos} = render_docs(Content, [Tag|State], Pos, 0, D),
trimnlnl({["##### ", Docs, ial(Attr)], NewPos});
render_element({Tag = h6, Attr, Content}, State, Pos, _Ind, D) when Pos =< 2 ->
{Docs, NewPos} = render_docs(Content, [Tag|State], Pos, 0, D),
trimnlnl({["###### ", Docs, ial(Attr)], NewPos});
render_element({pre, _Attr, _Content} = E, State, Pos, Ind, D) when Pos > Ind ->
%% We pad `pre` with two newlines if the previous section did not indent the region.
{Docs, NewPos} = render_element(E, State, 0, Ind, D),
{["\n\n", Docs], NewPos};
render_element({br, _Attr, _Content}, [td|_State], Pos, _Ind, _D) ->
{" ", Pos + 1};
render_element({br, _Attr, _Content} = E, State, Pos, Ind, D) when Pos > Ind ->
{Docs, NewPos} = render_element(E, State, 0, Ind, D),
{[" \n", Docs], NewPos};
render_element({p, _Attr, _Content} = E, State, Pos, Ind, D) when Pos > Ind ->
{Docs, NewPos} = render_element(E, State, 0, Ind, D),
{["\n\n", Docs], NewPos};
render_element({Elem, _Attr, _Content} = E, State, Pos, Ind, D) when Pos > Ind, ?IS_BLOCK(Elem) ->
{Docs, NewPos} = render_element(E, State, 0, Ind, D),
{["\n", Docs], NewPos};
render_element({'div', [{class, What}], Content}, State, Pos, Ind, D) ->
Type = case What of
<<"warning">> -> What;
<<"error">> -> What;
<<"note">> -> <<"info">>;
<<"change">> -> <<"info">>;
<<"do">> -> <<"tip">>;
<<"dont">> -> <<"error">>
end,
Title = unicode:characters_to_binary([string:titlecase(What), " ",ial([{class,Type}])]),
{Header, 0} = render_element({h4, [], [Title]}, State, Pos, Ind, D),
{Docs, 0} = render_element({'div', [], Content}, ['div' | State], 0, 0, D),
trimnlnl([pad(Ind - Pos), "> ", string:trim(Header), "\n",
[[pad(Ind), string:trim(["> ",Line]),"\n"] || Line <- string:split([trim(Docs)],"\n",all)]]);
render_element({Tag, _Attr, Content}, State, Pos, Ind, D) when Tag =:= p; Tag =:= 'div' ->
trimnlnl(render_docs(Content, [Tag | State], Pos, Ind, D));
%% render_element({a, [{id,_Id}|_], []} = A, State, Pos, Ind, D) when Pos > 0 ->
%% {Docs, NewPos} = render_element(A, State, 0, Ind, D),
%% {["\n",Docs], NewPos};
render_element(Elem, State, Pos, Ind, D) when Pos < Ind ->
{Docs, NewPos} = render_element(Elem, State, Ind, Ind, D),
{[pad(Ind - Pos), Docs], NewPos};
render_element({a, Ids, []}, _State, Pos, _Ind, _D) ->
trimnl({[["[]()",ial([{id,render_elink_anchor(Id)}])] || {id,Id} <- Ids], Pos});
render_element({a, Attr, Content}, State, Pos, Ind, D) ->
{Docs, NewPos} = render_docs(Content, [a|State], Pos, Ind, D),
Id =
case proplists:get_all_values(id, Attr) of
[] -> "";
[IdStr] -> ial([{id,IdStr}])
end,
{[render_link(Attr, Docs),Id],NewPos};
render_element({code, _, Content}, [pre | _] = State, Pos, Ind, D) ->
%% When code is within a pre we don't emit any underline
render_docs(Content, [code | State], Pos, Ind, D);
%% Faulty {code,..} generated by diameter containing links.
%% we split them into multiple code segments.
render_element({code,CodeAttr,[Content,{a,AAttr,AContent}|H]}, State, Pos, Ind, D) ->
AttrWithoutId = proplists:delete(id,CodeAttr),
render_docs([{code,AttrWithoutId,[Content]},{a,AAttr,[{code,AttrWithoutId,AContent}]},
{code,CodeAttr,H}], State, Pos, Ind, D);
render_element({code,_CodeAttr,[]}, _State, Pos, _Ind, _D) ->
{"", Pos};
render_element({code, Attr, Content}, State, Pos, Ind, D) ->
{Docs, NewPos} = render_docs(Content, [code | State], Pos, Ind, D),
IsDocumented = fun(What, #docs_v1{ docs = V1Docs }) ->
case lists:keyfind(What, 1, V1Docs) of
{What, _, _, #{}, _} ->
true;
_ ->
false
end
end,
%% Try to convert code segments that refer to types but don't have a link
%% to have the correct prefix. i.e. <c>byte()</c> should be `t:byte()`.
TypedDocs =
maybe
%% We do not do any transform if we are in an `a` already
true ?= State =:= [] orelse a =/= hd(State),
{ok, T, _} ?= erl_scan:string(unicode:characters_to_list([Docs,"."]), {1, 1}),
{ok, [{call,_,{atom,_,Name},Args}]} ?=
case erl_parse:parse_exprs(T) of
{ok, [{op,A,'/',F,{integer,_,NumArgs}}]} ->
%% Translate any byte/0 to byte()
{ok,[{call,A,F,lists:duplicate(NumArgs,a)}]};
Else ->
Else
end,
case IsDocumented({function, Name, length(Args)}, D#config.docs) orelse
erl_internal:bif(Name, length(Args))
of
true when length(Args) =:= 0 ->
lists:concat([io_lib:write_atom(Name),"/",length(Args)]);
true ->
%% This is a function, so return code as is
{lists:concat(["[`",Docs,"`](`",io_lib:write_atom(Name),"/",length(Args),"`)"]), NewPos};
false when length(Args) =:= 1,
element(1, hd(Args)) =:= integer,
element(3, hd(Args)) =:= 3 ->
%% Is a foo(3) link
try
Name:module_info(), %% Check if module exists
NameStr = io_lib:write_atom(Name),
{["`m:",NameStr,"`"], Pos + string:length(NameStr)}
catch error:undef ->
Docs
end;
false when length(Args) =:= 1,
element(1, hd(Args)) =:= integer,
element(3, hd(Args)) =:= 1 ->
%% Is a foo(1) link, i.e. a seecom
NameStr = io_lib:write_atom(Name),
{["[",NameStr,"](",NameStr,"_cmd.md)"], Pos + string:length(NameStr)};
false ->
try
%% This is an op type (such as <c>=:=/2</c>)
erl_internal:op_type(Name, length(Args)),
{lists:concat(["[`",Docs,"`](`erlang:",io_lib:write_atom(Name),"/",length(Args),"`)"]), NewPos}
catch error:function_clause ->
case IsDocumented({type,Name,length(Args)}, D#config.docs) orelse
erl_internal:is_type(Name,length(Args)) of
true when length(Args) =:= 0 ->
lists:concat(["t:",io_lib:write_atom(Name),"/",length(Args)]);
true ->
%% This is a type, add type prefix
{lists:concat(["[`",Docs,"`](`t:",io_lib:write_atom(Name),"/",length(Args),"`)"]), NewPos};
false ->
case IsDocumented({callback,Name,length(Args)}, D#config.docs) of
true ->
%% This is a callback
{lists:concat(["[`",Docs,"`](`c:",io_lib:write_atom(Name),"/",length(Args),"`)"]), NewPos};
false ->
%% This is not a type, nor a function, nor a callback
Docs
end
end
end
end
else
%% Could be a remote type erlang:message_queue_data()
{ok, [{call,_,{remote,_,{atom,_,RM},{atom,_,RF}},RArgs}]} ->
case code:get_doc(RM) of
{ok, RemoteDocs} ->
case IsDocumented({function,RF,length(RArgs)}, RemoteDocs) of
true ->
%% This is a remote function
Docs;
false ->
case IsDocumented({type,RF,length(RArgs)}, RemoteDocs) of
true ->
%% This is a valid remote type
{lists:concat(
["[`",Docs,"`](`t:",io_lib:write_atom(RM),":",
io_lib:write_atom(RF),"/",length(RArgs),"`)"]),
NewPos};
false ->
Docs
end
end;
_ ->
%% Could not fetch docs
Docs
end;
%% Could be a callback Module:init()
{ok, [{call,_,{remote,_,{var,_,_RM},{atom,_,RF}},RArgs}]} ->
case IsDocumented({callback,RF,length(RArgs)}, D#config.docs) of
true ->
%% This is a callback
{lists:concat(["[`",Docs,"`](`c:",io_lib:write_atom(RF),"/",length(RArgs),"`)"]), NewPos};
false ->
Docs
end;
false ->
%% We are in a link already, maybe strip trailing (1/3)
case re:run(Docs, "^([a-z_]+)\\([13]\\)$",[{capture,all_but_first,list}, unicode]) of
{match,[MaybeMod]} ->
case code:which(list_to_atom(MaybeMod)) of
non_existing ->
Docs;
_ ->
MaybeMod
end;
_ ->
Docs
end;
_ ->
%% Could not parse
Docs
end,
if is_tuple(TypedDocs) ->
TypedDocs;
true ->
case re:run(TypedDocs, "`+", [global,unicode]) of
nomatch ->
{["`", TypedDocs, "`", ial(Attr)], NewPos};
{match,Matches} ->
LargestMatch = lists:max([Size || [{_, Size}] <- Matches]),
Ticks = lists:duplicate(LargestMatch+1,$`),
{[Ticks," ", TypedDocs, " ",Ticks,ial(Attr)], NewPos}
end
end;
render_element({em, Attr, Content}, State, Pos, Ind, D) ->
render_element({i, Attr, Content}, State, Pos, Ind, D);
render_element({i, Attr, Content}, State, Pos, Ind, D) ->
{Docs, NewPos} = render_docs(Content, [i | State], Pos, Ind, D),
case lists:member(pre, State) of
true ->
{[Docs], NewPos};
false ->
{["*", Docs, "*",ial(Attr)], NewPos}
end;
render_element({hr, [], []}, _State, Pos, _Ind, _D) ->
{"---\n", Pos};
render_element({br, [], []}, _State, Pos, _Ind, _D) ->
{"", Pos};
render_element({strong, Attr, Content}, State, Pos, Ind, D) ->
render_element({b, Attr, Content}, State, Pos, Ind, D);
render_element({b, Attr, Content}, State, Pos, Ind, D) ->
{Docs, NewPos} = render_docs(Content, State, Pos, Ind, D),
case lists:member(pre, State) of
true ->
{[Docs], NewPos};
false ->
{["__", Docs, "__",ial(Attr)], NewPos}
end;
render_element({pre, [], [{code,Attr,Content}]}, State, Pos, Ind, D) ->
render_element({pre, Attr, Content}, State, Pos, Ind, D);
render_element({pre, Attr, Content}, State, Pos, Ind, D) ->
%% This is a pre without any links or emphasis, so we use markdown
%% For pre we make sure to respect the newlines in pre
{Docs, _} = trimnl(render_docs(strip_tags(Content), [pre | State], Pos, Ind, D)),
Type =
case unicode:characters_to_binary(proplists:get_value(type, Attr, "text")) of
<<"none">> -> "text";
<<"text">> -> "text";
<<"erlang">> -> "erlang";
<<"erl">> -> "erlang";
<<"erl-repl">> -> "erlang";
<<"c">> -> "c"
end,
IdAttr = proplists:delete(type, Attr),
trimnlnl(["```",Type,"\n", pad(Ind), Docs, pad(Ind), "```",
[["\n",pad(Ind),ial(IdAttr)] || IdAttr =/= []]]);
render_element({ul, [{class, <<"types">>}], _Content}, _State, Pos, _Ind, _D) ->
case _D#config.current of
{_, _, _, _, #{ specs := _}} ->
{"", Pos};
_ ->
{Docs, _} = render_docs(_Content, [types | _State], 0, _Ind, _D),
trimnlnl(Docs)
end;
render_element({li, Attr, Content}, [types | _] = State, Pos, Ind, C) ->
Doc =
case {proplists:get_value(name, Attr), proplists:get_value(class, Attr)} of
{undefined, Class} when Class =:= undefined; Class =:= <<"type">> ->
%% Inline html for types
render_docs(Content ++ [<<" ">>], [type | State], Pos, Ind, C);
{_, <<"description">>} ->
%% Inline html for type descriptions
render_docs(Content ++ [<<" ">>], [type | State], Pos, Ind + 2, C);
{Name, _} ->
%% Try to render from type metadata
case render_type_signature(binary_to_atom(Name), C) of
undefined when Content =:= [] ->
%% Failed and no content, emit place-holder
{["```erlang\n-type ", Name, "() :: term().```"], 0};
undefined ->
%% Failed with metadata, render the content
render_docs(Content ++ [<<" ">>], [type | State], Pos, Ind, C);
Type ->
%% Emit the erl_pp typespec
{["```erlang\n", Type, "```"], 0}
end
end,
trimnl(Doc);
render_element({ul, [], Content}, State, Pos, Ind, D) ->
trimnlnl(render_docs(Content, [ul | State], Pos, Ind, D));
render_element({ol, [], Content}, State, Pos, Ind, D) ->
trimnlnl(render_docs(Content, [ol | State], Pos, Ind, D));
render_element({li, [], Content}, [ul | _] = State, Pos, Ind, D) ->
{Docs, _NewPos} = render_docs(Content, [li | State], Pos + 2, Ind + 2, D),
trimnl(["* ", Docs]);
render_element({li, [], Content}, [ol | _] = State, Pos, Ind, D) ->
{Docs, _NewPos} = render_docs(Content, [li | State], Pos + 2, Ind + 2, D),
trimnl(["1. ", Docs]);
render_element({dl, [], [{dt,DTAttr,DTContent}, {dd,_,_} = DD1, {dd, _, _} = DD2 | Content]}, State, Pos, Ind, D) ->
{DD, T} = lists:splitwith(fun(E) -> element(1,E) =:= dd end, Content),
DDs = [{p, [], C} || {_, _, C} <- [DD1, DD2 | DD]],
render_element({dl, [], [{dt,DTAttr,DTContent}, {dd,[],DDs} | T]}, State, Pos, Ind, D);
render_element({dl, [], [{dt,DTAttr,DTContent}, {dd,[],DDContent} | Content]}, State, Pos, Ind, D) ->
Since = proplists:get_value(since, DTAttr),
{DTDocs, _DTNewPos} =
render_docs(
[{b, [], DTContent}],
[li, dl | State],
Pos + 2,
Ind + 2,
D),
Ids = [{id,Id} || {id,Id} <- DTAttr],
DTDocsWAnchors = case Ids of
[] -> trim(DTDocs);
Ids -> [trim(DTDocs),ial(Ids)]
end,
{DDDocs, DDNewPos} = render_docs(DDContent, [li, dd | State], 0, Ind + 2, D),
{Docs, NewPos} =
case string:find(DTDocs, "\n") of
nomatch when Since =:= undefined, is_binary(hd(DDContent)) orelse element(1,hd(DDContent)) =/= pre ->
trimnlnl({["* ", trim(DTDocsWAnchors), " - ", string:trim(string:trim(DDDocs, both, "\n"), leading, " ")], DDNewPos});
_ ->
trimnlnl({["* ", trim(DTDocsWAnchors), [["(Since ",Since,")"] || Since =/= undefined]," \n",
DDDocs], DDNewPos})
end,
{DLDocs, DLPos} = render_element({dl, [], Content}, State, NewPos, Ind, D),
{[Docs,DLDocs], DLPos};
render_element({dl, [], []}, _State, Pos, _Ind, _D) ->
{"", Pos};
render_element({table, Attr, Rows}, State, Pos, Ind, D) ->
[{tr,_,Head} | RowsNoCaption] = [Row || {tr,_,_} = Row <- Rows],
{TableDocs, TablePos} =
trimnl(render_docs([{th, [], Head} | RowsNoCaption], [table|State], Pos, Ind, D)),
{CaptionDocs, CaptionPos} =
render_docs([{em, [], [<<"Table: ">>, C]} || {caption,_,C} <- Rows, not string:equal(C,"")],
[table|State], TablePos, Ind, D),
trimnlnl({[TableDocs, [[pad(Ind),ial(proplists:delete(align,Attr)),"\n\n"] || Attr =/= []], CaptionDocs], CaptionPos});
render_element({th, [], Head}, State, _Pos, _Ind, D) ->
Header =
[begin {Docs, _} = render_docs(Td, [th|State], 0, 0, D),
{["| ", Docs, " "], ["|-", lists:duplicate(string:length(Docs), $-), "-"]}
end || Td <- Head],
trimnl({[[ Docs || {Docs,_} <- Header ], "|\n",
[ Lines || {_, Lines} <- Header ], "|\n"], 0});
render_element({tr, [], Row}, State, _Pos, _Ind, D) ->
Rows =
[begin {Docs, _} = render_docs(Td, [tr|State], 0, 0, D),
["| ", Docs, " "]
end || Td <- Row],
trimnl({[ Rows, "|"], 0});
render_element({td, _, TDContent}, State, Pos, Ind, D) ->
render_docs(TDContent, [td|State], Pos, Ind, D);
render_element({img,Attr,Content}, _State, Pos, _Ind, _D) ->
Caption = case lists:keyfind(caption, 1, Content) of
false -> "";
{caption, _, C} ->
C
end,
trimnlnl({["![",Caption,"](",filename:join("assets",filename:basename(proplists:get_value(file,Attr))),
" \"",Caption,"\")",ial(proplists:delete(file, Attr)),"\n"], Pos});
render_element({quote, [], Content}, State, _Pos, Ind, D) ->
{Docs, 0} = render_element({'div', [], Content}, ['div' | State], 0, 0, D),
trimnlnl([[pad(Ind), "> ",Line,"\n"] || Line <- string:split(trim(Docs),"\n",all)]);
render_element(B, State, Pos, Ind, _D) when is_binary(B) ->
%% Indent the string correctly
Pre = re:replace(B,"\n",nlpad(Ind),[global,unicode]),
Str =
case State of
[pre | _] ->
Pre;
[code | _] ->
Pre;
[h4 | _] ->
Pre;
_ ->
EscapeChars = [
"\\",
"`",
"{",
"}",
"!"|
[["|"] || lists:member(table,State)]
],
lists:foldl(
fun({Pat, Subst}, S) -> re:replace(S, Pat, Subst, [global,unicode]) end,
B,
[{["(", lists:join($|, [["\\", C] || C <- EscapeChars]), ")"], "\\\\\\1"},
%% [^S\r\n] == All whitespace except \r\n
{"(\n\\s*[0-9]+)\\.([^S\r\n])", "\\1\\\\.\\2"}, %% \n1. -> 1\.
{"^(\\s*[0-9]+)\\.([^S\r\n])", "\\1\\\\.\\2"}, %% ^1. -> 1\.
{"(\n\\s*)\\*([^S\r\n])", "\\1\\\\*\\2"}, %% \n* -> \*
{"^(\\s*)\\*([^S\r\n])", "\\1\\\\*\\2"}, %% ^* -> \*
{"(\n\\s*)\\-([^S\r\n])", "\\1\\\\-\\2"}, %% \n- -> \-
{"^(\\s*)\\-([^S\r\n])", "\\1\\\\-\\2"}, %% ^- -> \-
{"(\n\\s*)\\+([^S\r\n])", "\\1\\\\+\\2"}, %% \n+ -> \+
{"^(\\s*)\\+([^S\r\n])", "\\1\\\\+\\2"}, %% ^+ -> \+
{"(\n\\s*)\\#([^S\r\n])", "\\1\\\\#\\2"}, %% \n# -> \#
{"^(\\s*)\\#([^S\r\n])", "\\1\\\\#\\2"}, %% ^# -> \#
{"\\[([^]]+\\])", "\\\\[\\1"}, %% [..] -> \[..]
{"<(http[^>]+>)", "\\\\<\\1"}, %% <..> -> \<..>
{"(\s)_([^_]+_\s)", "\\1\\\\_\\2"}] %% _.._ -> \_.._
)
end,
{Str, Pos + lastline(Str)};
render_element({Tag, Attr, Content}, State, Pos, Ind, D) ->
case lists:member(Tag, ?ALL_ELEMENTS) of
true ->
throw({unhandled_element, Tag, Attr, Content});
false ->
throw({unknown_element, Tag, Attr, Content}),
ok
end,
render_docs(Content, State, Pos, Ind, D).
render_link(Attr, Docs) ->
render_link(Docs, proplists:get_value(rel, Attr), proplists:get_value(href, Attr)).
render_link(Docs, undefined, Href) when Href =/= undefined ->
%% This types of links are usually from edoc, but could also be
%% <url> from erl_docgen
case Href of
<<"overview-summary.html",Rest/binary>> ->
%% This is an edoc overview anchor
Anchor = lists:last(string:split(Rest,"#")),
["[", Docs, "](chapter.md#", render_link_anchor(Anchor), ")"];
Href ->
["[", Docs, "](", Href, ")"]
end;
render_link(Docs, <<"https://erlang.org/doc/link/seemfa">>, Href) ->
MFA = case string:split(Href, ":") of
[_App, HrefMFA] -> HrefMFA;
[Href] -> Href
end,
[Mod, FA] = case string:split(MFA, "#") of
[<<>>, MFANoAnchor] -> ["", MFANoAnchor];
[Module, FunArgs] ->
case string:equal(atom_to_list(get(module)), Module) of
true ->
["",FunArgs];
false ->
[[Module,":"],FunArgs]
end
end,
{Prefix, Func, Arity} =
case string:split(FA, "/") of
[<<"Module:", F/binary>>, A] ->
{"c:",F, A};
[<<"Mod:", F/binary>>, A] ->
{"c:",F, A};
[F, A] ->
{"", F, A}
end,
Link = [Mod,Func,"/",Arity],
case string:equal(Docs, Link) orelse string:equal(Docs, ["`",Link,"`"]) of
true ->
["`",Prefix,Link,"`"];
false ->
[
"[", Docs, "](`",Prefix,Link,"`)"
]
end;
render_link(Docs, <<"https://erlang.org/doc/link/seetype">>, Href) ->
MFA = case string:split(Href, ":") of
[_App, HrefMFA] -> HrefMFA;
[Href] -> Href
end,
[ModDocs, Mod, FA] =
case string:split(MFA, "#") of
[<<>>, MFANoAnchor] -> [get(module), "", MFANoAnchor];
[Module, FunArgs] ->
case string:equal(atom_to_list(get(module)), Module) of
true ->
[get(module), "",FunArgs];
false ->
[binary_to_atom(Module), [Module,":"],FunArgs]
end
end,
{Func, Arity} =
case string:split(FA, "/") of
[FA] ->
{ok, #docs_v1{ docs = Ds}} = code:get_doc(ModDocs),
App = get(application),
case lists:search(
fun(E) ->
case element(1, E) of
{type, Type, _} ->
string:equal(atom_to_list(Type), FA);
_ ->
false
end
end, lists:sort(Ds)) of
{value, {{type,_,TypeArity},_,_,_,_}} ->
{FA, integer_to_list(TypeArity)};
_Else when App =/= "wx" ->
io:format("Could not find find type: ~p~n",
[[Mod, FA]]),
exit({Mod, FA});
_Else ->
{FA,"0"}
end;
[F, A] ->
{F, A}
end,
Link = [Mod,Func,"/",Arity],
ZeroLink = [Mod,Func,"()"],
case (string:equal(Docs, Link) orelse string:equal(Docs, ["`",Link,"`"])) orelse
((string:equal(Docs, ZeroLink) orelse string:equal(Docs, ["`",ZeroLink,"`"])) andalso Arity =:= "0")
of
true ->
["`t:", Link, "`"];
false ->
[
"[", Docs, "](`t:", Link,"`)"
]
end;
render_link(Docs, <<"https://erlang.org/doc/link/seeerl">>,
Href = <<"erl_docgen:doc_storage">>) ->
["[",Docs,"](`e:",Href,".md`)"];
render_link(Docs, <<"https://erlang.org/doc/link/seeerl">>,
<<"erl_docgen:doc_storage.html">>) ->
["[",Docs,"](`e:erl_docgen:doc_storage.md`)"];
render_link(Docs, <<"https://erlang.org/doc/link/seeerl">>,
<<"edoc:edoc_cmd">>) ->
["[",Docs,"](edoc_cmd.md)"];
render_link(Docs, <<"https://erlang.org/doc/link/seeerl">>, Href) ->
ModAnchor =
case string:split(Href, ":") of
[MA] ->
MA;
[_App, MA] ->
MA
end,
ModFixedAnchor =
case string:split(ModAnchor, "#") of
[ModAnchor] ->
ModAnchor;
[M, A] ->
[M, "#", render_elink_anchor(A)]
end,
DocsNoMan3 = re:replace(Docs,["(`?",ModFixedAnchor,")\\(3\\)(`?)"],"\\1\\2"),
case string:equal(DocsNoMan3, ModFixedAnchor) orelse
string:equal(DocsNoMan3, ["`",ModFixedAnchor,"`"]) of
true ->
["`m:", ModFixedAnchor, "`"];
false ->
["[", Docs, "](`m:", ModFixedAnchor, "`)"]
end;
render_link(Docs, <<"https://erlang.org/doc/link/seeguide">>, Href) ->
CurrentApplication = unicode:characters_to_binary(get(application)),
RemoveSystemApp = fun(<<"system/general_info",_/binary>>) ->
<<"general_info">>;
(<<"system",_/binary>>) ->
<<"system">>;
(Else) ->
Else
end,
case string:lexemes(Href, ":#") of
[App, <<"index">>] when App =:= CurrentApplication ->
["[", Docs, "](index.html)"];
[App, <<"index">>] ->
["[", Docs, "](`e:",RemoveSystemApp(App),":index.html`)"];
[App, Guide] when App =:= CurrentApplication ->
["[", Docs, "](",string:lowercase(Guide),".md)"];
[App, Guide, Anchor] when App =:= CurrentApplication ->
["[", Docs, "](",string:lowercase(Guide),".md#",
render_elink_anchor(Anchor),")"];
[App, Guide] ->
["[", Docs, "](`e:",RemoveSystemApp(App),":",string:lowercase(Guide),".md`)"];
[App, Guide, Anchor] ->
["[", Docs, "](`e:",RemoveSystemApp(App),":",string:lowercase(Guide),".md#",
render_elink_anchor(Anchor),"`)"]
end;
render_link(Docs, Rel, Href)
when Rel =:= <<"https://erlang.org/doc/link/seecref">>;
Rel =:= <<"https://erlang.org/doc/link/seecom">>;
Rel =:= <<"https://erlang.org/doc/link/seeapp">> ->
CurrentApplication = unicode:characters_to_binary(get(application)),
Postfix = case Rel of
<<"https://erlang.org/doc/link/seecom">> ->
"_cmd";
<<"https://erlang.org/doc/link/seeapp">> ->
"_app";
_ ->
""
end,
AddPostfix = fun(Guide) ->
string:lowercase(
case string:prefix(string:reverse(Guide),
string:reverse(Postfix)) of
nomatch ->
[Guide,Postfix];
_ ->
Guide
end)
end,
case string:lexemes(Href, ":#") of
[App, <<"index">>] when App =:= CurrentApplication ->
["[", Docs, "](index.html)"];
[App, <<"index">>] ->
["[", Docs, "](`e:",App,":index.html`)"];
[App, Guide] when App =:= CurrentApplication ->
["[", Docs, "](",AddPostfix(Guide),".md)"];
[App, Guide, Anchor] when App =:= CurrentApplication ->
["[", Docs, "](",AddPostfix(Guide),".md#",
render_elink_anchor(Anchor),")"];
[App, Guide] ->
["[", Docs, "](`e:",App,":",AddPostfix(Guide),".md`)"];
[App, Guide, Anchor] ->
["[", Docs, "](`e:",App,":",AddPostfix(Guide),".md#",
render_elink_anchor(Anchor),"`)"]
end;
render_link(Docs, <<"https://erlang.org/doc/link/seefile">>, Href) ->
CurrentApplication = unicode:characters_to_binary(get(application)),
MaybeAddExtension = fun(G) ->
string:lowercase(
case string:equal(filename:extension(G),"") of
true -> [G,".md"];
_ -> G
end)
end,
case string:lexemes(Href, ":#") of
[App, Guide] when App =:= CurrentApplication, App =:= <<"jinterface">> ->
["[", Docs, "](assets/",Guide,".html)"];
[App, Guide, Anchor] when App =:= CurrentApplication, App =:= <<"jinterface">> ->
["[", Docs, "](assets/",Guide,".html#",render_link_anchor(Anchor),")"];
[App, Guide] when App =:= CurrentApplication ->
["[", Docs, "](",MaybeAddExtension(Guide),")"];
[App, Guide, Anchor] when App =:= CurrentApplication ->
["[", Docs, "](",MaybeAddExtension(Guide),"#",render_link_anchor(Anchor),")"];
[App, Guide] ->
["[", Docs, "](`e:",App,":",MaybeAddExtension(Guide),"`)"];
[App, Guide, Anchor] ->
["[", Docs, "](`e:",App,":",MaybeAddExtension(Guide),
"#",render_link_anchor(Anchor),"`)"]
end;
render_link(Docs, _Rel, _Href) ->
Docs.
render_elink_anchor(Anchor) ->
render_link_anchor(
lists:foldl(
fun({Re,Sub}, Str) -> re:replace(Str, Re, Sub, [global, unicode]) end,
Anchor,
[{" ","-"},{"(--|\\.)","-"}, {"(^-|-$)",""}])).
render_link_anchor(Anchor) ->
uri_string:quote(Anchor).
-spec render_type_signature(atom(), #config{}) -> 'undefined' | unicode:chardata().
render_type_signature(Name, #config{docs = #docs_v1{metadata = #{types := AllTypes}}}) ->
case [Type || Type = {TName, _} <- maps:keys(AllTypes), TName =:= Name] of
[] ->
undefined;
Types ->
[erl_pp:attribute(maps:get(Type, AllTypes)) || Type <- Types]
end.
ial([]) ->
"";
ial(Attrs) ->
["{: ", [[ial(Tag, Value), " "] || {Tag,Value} <- Attrs], "}"].
ial(class, Value) ->
[".", maybe_quote_ial(Value)];
ial(id, Value) ->
["#", maybe_quote_ial(Value)];
ial(Tag, Value) ->
[atom_to_list(Tag), "=", maybe_quote_ial(Value)].
maybe_quote_ial(Str) ->
case string:find(Str, " ") of
nomatch ->
Str;
_ ->
[$",Str,$"]
end.
%% Pad N spaces (and possibly pre-prend newline), disabling any ansi formatting while doing so.
-spec pad(non_neg_integer()) -> unicode:chardata().
pad(N) ->
pad(N, "").
-spec nlpad(non_neg_integer()) -> unicode:chardata().
nlpad(N) ->
pad(N, "\n").
-spec pad(non_neg_integer(), unicode:chardata()) -> unicode:chardata().
pad(N, Extra) ->
Pad = lists:duplicate(N, [$\s]),
[Extra, Pad].
-spec lastline(unicode:chardata()) -> non_neg_integer().
%% Look for the length of the last line of a string
lastline(Str) ->
LastStr =
case string:find(Str, "\n", trailing) of
nomatch ->
Str;
Match ->
tl(string:next_codepoint(Match))
end,
string:length(LastStr).
%% These functions make sure that we trim extra newlines added
%% by the renderer. For example if we do <li><p></p></li>
%% that would add 4 \n at after the last </li>. This is trimmed
%% here to only be 2 \n
-spec trimnlnl(unicode:chardata() | {unicode:chardata(), non_neg_integer()}) ->
{unicode:chardata(), 0}.
trimnlnl({Chars, _Pos}) ->
nl(nl(string:trim(Chars, trailing, "\n")));
trimnlnl(Chars) ->
nl(nl(string:trim(Chars, trailing, "\n"))).
-spec trimnl(unicode:chardata() | {unicode:chardata(), non_neg_integer()}) ->
{unicode:chardata(), 0}.
trimnl({Chars, _Pos}) ->
nl(string:trim(Chars, trailing, "\n"));
trimnl(Chars) ->
nl(string:trim(Chars, trailing, "\n")).
trim(Chars) ->
string:trim(Chars, trailing, "\n").
-spec nl(unicode:chardata() | {unicode:chardata(), non_neg_integer()}) -> {unicode:chardata(), 0}.
nl({Chars, _Pos}) ->
nl(Chars);
nl(Chars) ->
{[Chars, "\n"], 0}.
%%======================================================================
%% Records
%%======================================================================
%%----------------------------------------------------------------------
%% State record for the validator
%%----------------------------------------------------------------------
-record(state, {
tags=[], %% Tag stack
dom=[] %% DOM structure
}).
%%======================================================================
%% External functions
%%======================================================================
%%----------------------------------------------------------------------
%% Function: initial_state() -> Result
%% Parameters:
%% Result:
%% Description:
%%----------------------------------------------------------------------
initial_state() ->
#state{}.
%%----------------------------------------------------------------------
%% Function: get_dom(State) -> Result
%% Parameters:
%% Result:
%% Description:
%%----------------------------------------------------------------------
get_dom(#state{dom=Dom}) ->
Dom.
%%----------------------------------------------------------------------
%% Function: event(Event, LineNo, State) -> Result
%% Parameters:
%% Result:
%% Description:
%%----------------------------------------------------------------------
event(Event, _LineNo, State) ->
build_dom(Event, State).
%%======================================================================
%% Internal functions
%%======================================================================
%%----------------------------------------------------------------------
%% Function : build_dom(Event, State) -> Result
%% Parameters: Event = term()
%% State = #xmerl_sax_simple_dom_state{}
%% Result : #xmerl_sax_simple_dom_state{} |
%% Description:
%%----------------------------------------------------------------------
%% Document
%%----------------------------------------------------------------------
build_dom(startDocument, State) ->
State#state{dom=[startDocument]};
build_dom(endDocument,
#state{dom=[{Tag, Attributes, Content} |D]} = State) ->
case D of
[startDocument] ->
State#state{dom=[{Tag, Attributes,