634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
...
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
...
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
|
end
function span_renderers.macro(m,b,s)
local macroname = plainrdr.htmlSpan(
ct.parse_span(m.macro, b.origin), b,s)
local r = b.origin:ref(macroname)
if type(r) ~= 'string' then
b.origin:fail('%s is an object, not a reference', t.ref)
end
local mctx = b.origin:clone()
mctx.invocation = m
return htmlSpan(ct.parse_span(r, mctx),b,s)
end
function span_renderers.math(m,b,s)
addStyle 'math'
................................................................................
end
end;
label = function(b,s)
if ct.sec.is(b.captions) then
if not (opts['fossil-uv'] or opts.snippet) then
addStyle 'header'
end
local h = math.min(6,math.max(1,b.captions.depth))
return tag(f('h%u',h), nil, sr.htmlSpan(b.spans, b, s), b)
else
-- handle other uses of labels here
end
end;
['list-item'] = function(b,s)
return tag('li', nil, sr.htmlSpan(b.spans, b, s), b)
................................................................................
end;
['break'] = function() -- HACK
-- lists need to be rewritten to work like asides
return '';
end;
}
function block_renderers.quote(b,s)
local ir = {}
local toIR = block_renderers
for i, sec in ipairs(b.doc.secorder) do
local secnodes = {}
for i, bl in ipairs(sec.blocks) do
if toIR[bl.kind] then
table.insert(secnodes, toIR[bl.kind](bl,sec))
end
end
if next(secnodes) then
if b.doc.secorder[2] then --#secs>1?
-- only wrap in a section if >1 section
table.insert(ir, tag('section',
{id = getSafeID(sec)},
secnodes))
else
ir = secnodes
end
end
end
return tag('blockquote', b.id and {id=getSafeID(b)} or {}, catenate(ir))
end
return block_renderers;
end
local function getRenderers(procs)
local span_renderers = getSpanRenderers(procs)
local r = getBaseRenderers(procs,span_renderers)
r.block_renderers = getBlockRenderers(procs, r)
return r
|
|
>
>
|
|
<
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
...
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
...
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
|
end
function span_renderers.macro(m,b,s)
local macroname = plainrdr.htmlSpan(
ct.parse_span(m.macro, b.origin), b,s)
local r = b.origin:ref(macroname)
if type(r) ~= 'string' then
b.origin:fail('%s is an object, not a reference', r.id)
end
local mctx = b.origin:clone()
mctx.invocation = m
return htmlSpan(ct.parse_span(r, mctx),b,s)
end
function span_renderers.math(m,b,s)
addStyle 'math'
................................................................................
end
end;
label = function(b,s)
if ct.sec.is(b.captions) then
if not (opts['fossil-uv'] or opts.snippet) then
addStyle 'header'
end
-- use correct styling in subdocuments
local visDepth = b.captions.depth + (b.origin.docDepth or 0)
local h = math.min(6,math.max(1,visDepth))
return tag(f('h%u',h), nil, sr.htmlSpan(b.spans, b, s), b)
else
-- handle other uses of labels here
end
end;
['list-item'] = function(b,s)
return tag('li', nil, sr.htmlSpan(b.spans, b, s), b)
................................................................................
end;
['break'] = function() -- HACK
-- lists need to be rewritten to work like asides
return '';
end;
}
local function renderSubdoc(doc)
local ir = {}
for i, sec in ipairs(doc.secorder) do
local secnodes = {}
for i, bl in ipairs(sec.blocks) do
if block_renderers[bl.kind] then
table.insert(secnodes, block_renderers[bl.kind](bl,sec))
end
end
if next(secnodes) then
if doc.secorder[2] then --#secs>1?
-- only wrap in a section if >1 section
table.insert(ir, tag('section',
{id = getSafeID(sec)},
secnodes))
else
ir = secnodes
end
end
end
return ir
end
local function flatten(t)
if t == nil then
return ''
elseif type(t) == 'string' then
return t
elseif type(t) == 'table' then
if t[1] then
return catenate(ss.map(flatten, t))
elseif t.tag then
return tag(t.tag, t.attrs or {}, flatten(t.nodes))
elseif t.elt then
return tag(t.elt, t.attrs or {})
end
end
end
function block_renderers.embed(b,s)
local obj
if b.rsrc
then obj = b.rsrc
else obj = b.origin:ref(b.ref)
end
local function htmlURI(u)
local family = u:canfetch()
if family == 'file' or
(family == 'http' and u.namespace == nil) then
-- TODO asset:
return u.path
else
return tostring(u)
end
end
local function uriForSource(s)
if s.mode == 'link' or s.mode == 'auto' then
return htmlURI(s.uri)
elseif s.mode == 'embed' then
local mime = s.mime:clone()
mime.opts = {}
return string.format('data:%s;base64,%s', mime, ss.str.b64e(s.raw))
end
end
--figure out how to embed the given object
local embedActs = {
{ss.mime'image/*', function(s,ctr)
if s == nil then
return {tag = "picture", nodes = {}}
else
local uri = uriForSource(s)
local fbimg, idx
if next(ctr.nodes) == nil then
idx = 1
fbimg = {
elt = 'img'; --fallback
attrs = {
alt = '';
src = uri;
};
}
else idx = #ctr.nodes end
table.insert(ctr.nodes, idx, {
elt = 'source'; --fallback
attrs = { srcset = uri; };
})
if fbimg then
table.insert(ctr.nodes,fbimg)
else
-- fallback <img> should be lowest-prio image
ctr.nodes[#ctr.nodes].attrs.src = uri;
end
end
end};
{ss.mime'text/x.cortav', function(s,ctr)
if s == nil then
return {}
elseif next(ctr) == nil then
if (s.mode == 'embed' or s.mode == 'auto') and s.doc then
ctr.tag = 'div'; -- kinda hacky, maybe fix
ctr.nodes = renderSubdoc(s.doc)
elseif s.mode == 'link' then
-- yeah this is not gonna work my dude
ctr.elt = 'embed';
ctr.attrs = {
type = 'text/x.cortav';
src = htmlURI(s.uri);
}
end
end
end};
{ss.mime'text/html', function(s,ctr)
if s == nil then
return {}
elseif next(ctr) == nil then
if (s.mode == 'embed' or s.mode == 'auto') and s.raw then
ctr.tag = 'div'
ctr.nodes = s.raw
elseif s.mode == 'link' then
ctr.elt = 'embed';
ctr.attrs = {
type = 'text/html';
src = htmlURI(s.uri);
}
end
end
end};
{ss.mime'text/*', function(s,ctr)
if s == nil then
return {}
elseif next(ctr) == nil then
local mime = s.mime:clone()
mime.opts={}
if (s.mode == 'embed' or s.mode == 'auto') and s.raw then
ctr.tag = 'pre';
ctr.nodes = s.raw
elseif s.mode == 'link' then
ctr.elt = 'embed';
ctr.attrs = {
type = tostring(mime);
src = htmlURI(s.uri);
}
end
end
end};
}
local rtype
local fallback
for n, src in ipairs(obj.srcs) do
if fallback == nil and (src.mode == 'link' or src.mode == 'auto') then
fallback = src
end
for i, ea in ipairs(embedActs) do
if ea[1] < src.mime then -- fits!
rtype = ea
goto compatFound
end
end
end
-- nothing found; install fallback link
if fallback then
local lnk = htmlURI(fallback.uri)
return tag('a', {href=lnk},
tag('div',{class=xref},
string.format("→ %s [%s]", b.cap or '', tostring(fallback.mime))))
else
addStyle 'docmeta'
return tag('div',{class="render-warn"},
'could not embed object type ' .. tostring(obj.srcs.mime))
end
::compatFound::
local top = rtype[2]() -- create container
for n, src in ipairs(obj.srcs) do
if rtype[1] < src.mime then
rtype[2](src, top)
end
end
local ft = flatten(top)
return ft
end
function block_renderers.macro(b,s)
local all = renderSubdoc(b.doc)
local cat = catenate(ss.map(flatten,all))
return tag('div', {}, cat)
end
function block_renderers.quote(b,s)
local ir = renderSubdoc(b.doc)
return tag('blockquote', b.id and {id=getSafeID(b)} or {}, catenate(ss.map(flatten,ir)))
end
return block_renderers
end
local function getRenderers(procs)
local span_renderers = getSpanRenderers(procs)
local r = getBaseRenderers(procs,span_renderers)
r.block_renderers = getBlockRenderers(procs, r)
return r
|