git.iko.soy/typeable/pandoc
on master help sign in
Make internal links work in ODT/OpenDocument.

This adds proper bookmarks to the headers with non-null IDs. Closes #4358.

commit2b89aaf04dd2d1bdafa0ee507abefd4f0d6df271 authorJohn MacFarlane <jgm@berkeley.edu> dateMon 14 May 2018, 10:37 (8 years ago) parent933aa3e
2 files changed+64−49
src/Text/Pandoc/Writers/OpenDocument.hs+11−5 test/writer.opendocument+53−44
src/Text/Pandoc/Writers/OpenDocument.hs+11−5
@@ -193,10 +193,15 @@ formulaStyle mt = inTags False "style:style"
193193 ,("style:horizontal-rel", "paragraph-content")
194194 ,("style:wrap", "none")]
195195
196−inHeaderTags :: PandocMonad m => Int -> Doc -> OD m Doc
197−inHeaderTags i d =
196+inHeaderTags :: PandocMonad m => Int -> String -> Doc -> OD m Doc
197+inHeaderTags i ident d =
198198 return $ inTags False "text:h" [ ("text:style-name", "Heading_20_" ++ show i)
199− , ("text:outline-level", show i)] d
199+ , ("text:outline-level", show i)]
200+ $ if null ident
201+ then d
202+ else selfClosingTag "text:bookmark-start" [ ("text:name", ident) ]
203+ <> d <>
204+ selfClosingTag "text:bookmark-end" [ ("text:name", ident) ]
200205
201206inQuotes :: QuoteType -> Doc -> Doc
202207inQuotes SingleQuote s = char '\8216' <> s <> char '\8217'
@@ -349,8 +354,9 @@ blockToOpenDocument o bs
349354 | LineBlock b <- bs = blockToOpenDocument o $ linesToPara b
350355 | Div attr xs <- bs = withLangFromAttr attr
351356 (blocksToOpenDocument o xs)
352− | Header i _ b <- bs = setFirstPara >>
353− (inHeaderTags i =<< inlinesToOpenDocument o b)
357+ | Header i (ident,_,_) b
358+ <- bs = setFirstPara >> (inHeaderTags i ident
359+ =<< inlinesToOpenDocument o b)
354360 | BlockQuote b <- bs = setFirstPara >> mkBlockQuote b
355361 | DefinitionList b <- bs = setFirstPara >> defList b
356362 | BulletList b <- bs = setFirstPara >> bulletListToOpenDocument o b
test/writer.opendocument+53−44
@@ -803,23 +803,31 @@
803803<text:p text:style-name="Text_20_body">This is a set of tests for pandoc. Most
804804of them are adapted from John Gruber’s markdown test suite.</text:p>
805805<text:p text:style-name="Horizontal_20_Line" />
806−<text:h text:style-name="Heading_20_1" text:outline-level="1">Headers</text:h>
807−<text:h text:style-name="Heading_20_2" text:outline-level="2">Level 2 with an
806+<text:h text:style-name="Heading_20_1" text:outline-level="1"><text:bookmark-start text:name="headers" />Headers<text:bookmark-end text:name="headers" /></text:h>
807+<text:h text:style-name="Heading_20_2" text:outline-level="2"><text:bookmark-start text:name="level-2-with-an-embedded-link" />Level
808+2 with an
808809<text:a xlink:type="simple" xlink:href="/url" office:name=""><text:span text:style-name="Definition">embedded
809−link</text:span></text:a></text:h>
810−<text:h text:style-name="Heading_20_3" text:outline-level="3">Level 3 with
811−<text:span text:style-name="T1">emphasis</text:span></text:h>
812−<text:h text:style-name="Heading_20_4" text:outline-level="4">Level 4</text:h>
813−<text:h text:style-name="Heading_20_5" text:outline-level="5">Level 5</text:h>
814−<text:h text:style-name="Heading_20_1" text:outline-level="1">Level 1</text:h>
815−<text:h text:style-name="Heading_20_2" text:outline-level="2">Level 2 with
816−<text:span text:style-name="T1">emphasis</text:span></text:h>
817−<text:h text:style-name="Heading_20_3" text:outline-level="3">Level 3</text:h>
810+link</text:span></text:a><text:bookmark-end text:name="level-2-with-an-embedded-link" /></text:h>
811+<text:h text:style-name="Heading_20_3" text:outline-level="3"><text:bookmark-start text:name="level-3-with-emphasis" />Level
812+3 with
813+<text:span text:style-name="T1">emphasis</text:span><text:bookmark-end text:name="level-3-with-emphasis" /></text:h>
814+<text:h text:style-name="Heading_20_4" text:outline-level="4"><text:bookmark-start text:name="level-4" />Level
815+4<text:bookmark-end text:name="level-4" /></text:h>
816+<text:h text:style-name="Heading_20_5" text:outline-level="5"><text:bookmark-start text:name="level-5" />Level
817+5<text:bookmark-end text:name="level-5" /></text:h>
818+<text:h text:style-name="Heading_20_1" text:outline-level="1"><text:bookmark-start text:name="level-1" />Level
819+1<text:bookmark-end text:name="level-1" /></text:h>
820+<text:h text:style-name="Heading_20_2" text:outline-level="2"><text:bookmark-start text:name="level-2-with-emphasis" />Level
821+2 with
822+<text:span text:style-name="T1">emphasis</text:span><text:bookmark-end text:name="level-2-with-emphasis" /></text:h>
823+<text:h text:style-name="Heading_20_3" text:outline-level="3"><text:bookmark-start text:name="level-3" />Level
824+3<text:bookmark-end text:name="level-3" /></text:h>
818825<text:p text:style-name="First_20_paragraph">with no blank line</text:p>
819−<text:h text:style-name="Heading_20_2" text:outline-level="2">Level 2</text:h>
826+<text:h text:style-name="Heading_20_2" text:outline-level="2"><text:bookmark-start text:name="level-2" />Level
827+2<text:bookmark-end text:name="level-2" /></text:h>
820828<text:p text:style-name="First_20_paragraph">with no blank line</text:p>
821829<text:p text:style-name="Horizontal_20_Line" />
822−<text:h text:style-name="Heading_20_1" text:outline-level="1">Paragraphs</text:h>
830+<text:h text:style-name="Heading_20_1" text:outline-level="1"><text:bookmark-start text:name="paragraphs" />Paragraphs<text:bookmark-end text:name="paragraphs" /></text:h>
823831<text:p text:style-name="First_20_paragraph">Here’s a regular
824832paragraph.</text:p>
825833<text:p text:style-name="Text_20_body">In Markdown 1.0.0 and earlier. Version
@@ -830,8 +838,8 @@ criminey.</text:p>
830838<text:p text:style-name="Text_20_body">There should be a hard line
831839break<text:line-break />here.</text:p>
832840<text:p text:style-name="Horizontal_20_Line" />
833−<text:h text:style-name="Heading_20_1" text:outline-level="1">Block
834−Quotes</text:h>
841+<text:h text:style-name="Heading_20_1" text:outline-level="1"><text:bookmark-start text:name="block-quotes" />Block
842+Quotes<text:bookmark-end text:name="block-quotes" /></text:h>
835843<text:p text:style-name="First_20_paragraph">E-mail style:</text:p>
836844<text:p text:style-name="P1">This is a block quote. It is pretty
837845short.</text:p>
@@ -855,8 +863,8 @@ short.</text:p>
8558632 &gt; 1.</text:p>
856864<text:p text:style-name="Text_20_body">And a following paragraph.</text:p>
857865<text:p text:style-name="Horizontal_20_Line" />
858−<text:h text:style-name="Heading_20_1" text:outline-level="1">Code
859−Blocks</text:h>
866+<text:h text:style-name="Heading_20_1" text:outline-level="1"><text:bookmark-start text:name="code-blocks" />Code
867+Blocks<text:bookmark-end text:name="code-blocks" /></text:h>
860868<text:p text:style-name="First_20_paragraph">Code:</text:p>
861869<text:p text:style-name="P9">---- (should be four hyphens)</text:p>
862870<text:p text:style-name="P10"></text:p>
@@ -870,8 +878,8 @@ Blocks</text:h>
870878<text:p text:style-name="P17"></text:p>
871879<text:p text:style-name="P18">These should not be escaped: <text:s text:c="1" />\$ \\ \&gt; \[ \{</text:p>
872880<text:p text:style-name="Horizontal_20_Line" />
873−<text:h text:style-name="Heading_20_1" text:outline-level="1">Lists</text:h>
874−<text:h text:style-name="Heading_20_2" text:outline-level="2">Unordered</text:h>
881+<text:h text:style-name="Heading_20_1" text:outline-level="1"><text:bookmark-start text:name="lists" />Lists<text:bookmark-end text:name="lists" /></text:h>
882+<text:h text:style-name="Heading_20_2" text:outline-level="2"><text:bookmark-start text:name="unordered" />Unordered<text:bookmark-end text:name="unordered" /></text:h>
875883<text:p text:style-name="First_20_paragraph">Asterisks tight:</text:p>
876884<text:list text:style-name="L2">
877885 <text:list-item>
@@ -944,7 +952,7 @@ Blocks</text:h>
944952 <text:p text:style-name="P24">Minus 3</text:p>
945953 </text:list-item>
946954</text:list>
947−<text:h text:style-name="Heading_20_2" text:outline-level="2">Ordered</text:h>
955+<text:h text:style-name="Heading_20_2" text:outline-level="2"><text:bookmark-start text:name="ordered" />Ordered<text:bookmark-end text:name="ordered" /></text:h>
948956<text:p text:style-name="First_20_paragraph">Tight:</text:p>
949957<text:list text:style-name="L8">
950958 <text:list-item>
@@ -1007,7 +1015,7 @@ Blocks</text:h>
10071015 <text:p text:style-name="P29">Item 3.</text:p>
10081016 </text:list-item>
10091017</text:list>
1010−<text:h text:style-name="Heading_20_2" text:outline-level="2">Nested</text:h>
1018+<text:h text:style-name="Heading_20_2" text:outline-level="2"><text:bookmark-start text:name="nested" />Nested<text:bookmark-end text:name="nested" /></text:h>
10111019<text:list text:style-name="L13">
10121020 <text:list-item>
10131021 <text:p text:style-name="P30">Tab</text:p><text:list text:style-name="L14">
@@ -1068,8 +1076,8 @@ paragraphs:</text:p>
10681076 <text:p text:style-name="P35">Third</text:p>
10691077 </text:list-item>
10701078</text:list>
1071−<text:h text:style-name="Heading_20_2" text:outline-level="2">Tabs and
1072−spaces</text:h>
1079+<text:h text:style-name="Heading_20_2" text:outline-level="2"><text:bookmark-start text:name="tabs-and-spaces" />Tabs
1080+and spaces<text:bookmark-end text:name="tabs-and-spaces" /></text:h>
10731081<text:list text:style-name="L20">
10741082 <text:list-item>
10751083 <text:p text:style-name="P37">this is a list item indented with
@@ -1089,8 +1097,8 @@ spaces</text:h>
10891097 </text:list>
10901098 </text:list-item>
10911099</text:list>
1092−<text:h text:style-name="Heading_20_2" text:outline-level="2">Fancy list
1093−markers</text:h>
1100+<text:h text:style-name="Heading_20_2" text:outline-level="2"><text:bookmark-start text:name="fancy-list-markers" />Fancy
1101+list markers<text:bookmark-end text:name="fancy-list-markers" /></text:h>
10941102<text:list text:style-name="L22">
10951103 <text:list-item>
10961104 <text:p text:style-name="P39">begins with 2</text:p>
@@ -1157,8 +1165,8 @@ item:</text:p>
11571165<text:p text:style-name="Text_20_body">M.A. 2007</text:p>
11581166<text:p text:style-name="Text_20_body">B. Williams</text:p>
11591167<text:p text:style-name="Horizontal_20_Line" />
1160−<text:h text:style-name="Heading_20_1" text:outline-level="1">Definition
1161−Lists</text:h>
1168+<text:h text:style-name="Heading_20_1" text:outline-level="1"><text:bookmark-start text:name="definition-lists" />Definition
1169+Lists<text:bookmark-end text:name="definition-lists" /></text:h>
11621170<text:p text:style-name="First_20_paragraph">Tight using spaces:</text:p>
11631171<text:p text:style-name="Definition_20_Term_20_Tight">apple</text:p>
11641172<text:p text:style-name="Definition_20_Definition_20_Tight">red fruit</text:p>
@@ -1225,8 +1233,8 @@ fruit</text:p><text:list text:style-name="L25">
12251233 <text:p text:style-name="P44">sublist</text:p>
12261234 </text:list-item>
12271235</text:list>
1228−<text:h text:style-name="Heading_20_1" text:outline-level="1">HTML
1229−Blocks</text:h>
1236+<text:h text:style-name="Heading_20_1" text:outline-level="1"><text:bookmark-start text:name="html-blocks" />HTML
1237+Blocks<text:bookmark-end text:name="html-blocks" /></text:h>
12301238<text:p text:style-name="First_20_paragraph">Simple block on one
12311239line:</text:p>
12321240<text:p text:style-name="Text_20_body">foo</text:p>
@@ -1262,8 +1270,8 @@ spaces on the line:</text:p>
12621270<text:p text:style-name="P50">&lt;hr /&gt;</text:p>
12631271<text:p text:style-name="First_20_paragraph">Hr’s:</text:p>
12641272<text:p text:style-name="Horizontal_20_Line" />
1265−<text:h text:style-name="Heading_20_1" text:outline-level="1">Inline
1266−Markup</text:h>
1273+<text:h text:style-name="Heading_20_1" text:outline-level="1"><text:bookmark-start text:name="inline-markup" />Inline
1274+Markup<text:bookmark-end text:name="inline-markup" /></text:h>
12671275<text:p text:style-name="First_20_paragraph">This is
12681276<text:span text:style-name="T1">emphasized</text:span>, and so
12691277<text:span text:style-name="T1">is this</text:span>.</text:p>
@@ -1300,8 +1308,9 @@ H<text:span text:style-name="T8">many of them</text:span>O.</text:p>
13001308<text:p text:style-name="Text_20_body">These should not be superscripts or
13011309subscripts, because of the unescaped spaces: a^b c^d, a~b c~d.</text:p>
13021310<text:p text:style-name="Horizontal_20_Line" />
1303−<text:h text:style-name="Heading_20_1" text:outline-level="1">Smart quotes,
1304−ellipses, dashes</text:h>
1311+<text:h text:style-name="Heading_20_1" text:outline-level="1"><text:bookmark-start text:name="smart-quotes-ellipses-dashes" />Smart
1312+quotes, ellipses,
1313+dashes<text:bookmark-end text:name="smart-quotes-ellipses-dashes" /></text:h>
13051314<text:p text:style-name="First_20_paragraph">“Hello,” said the spider.
13061315“‘Shelob’ is my name.”</text:p>
13071316<text:p text:style-name="Text_20_body">‘A’, ‘B’, and ‘C’ are letters.</text:p>
@@ -1319,7 +1328,7 @@ five.</text:p>
131913281987–1999.</text:p>
13201329<text:p text:style-name="Text_20_body">Ellipses…and…and….</text:p>
13211330<text:p text:style-name="Horizontal_20_Line" />
1322−<text:h text:style-name="Heading_20_1" text:outline-level="1">LaTeX</text:h>
1331+<text:h text:style-name="Heading_20_1" text:outline-level="1"><text:bookmark-start text:name="latex" />LaTeX<text:bookmark-end text:name="latex" /></text:h>
13231332<text:list text:style-name="L26">
13241333 <text:list-item>
13251334 <text:p text:style-name="P51"></text:p>
@@ -1371,8 +1380,8 @@ five.</text:p>
13711380</text:list>
13721381<text:p text:style-name="First_20_paragraph">Here’s a LaTeX table:</text:p>
13731382<text:p text:style-name="Horizontal_20_Line" />
1374−<text:h text:style-name="Heading_20_1" text:outline-level="1">Special
1375−Characters</text:h>
1383+<text:h text:style-name="Heading_20_1" text:outline-level="1"><text:bookmark-start text:name="special-characters" />Special
1384+Characters<text:bookmark-end text:name="special-characters" /></text:h>
13761385<text:p text:style-name="First_20_paragraph">Here is some unicode:</text:p>
13771386<text:list text:style-name="L28">
13781387 <text:list-item>
@@ -1415,8 +1424,8 @@ it.</text:p>
14151424<text:p text:style-name="Text_20_body">Plus: +</text:p>
14161425<text:p text:style-name="Text_20_body">Minus: -</text:p>
14171426<text:p text:style-name="Horizontal_20_Line" />
1418−<text:h text:style-name="Heading_20_1" text:outline-level="1">Links</text:h>
1419−<text:h text:style-name="Heading_20_2" text:outline-level="2">Explicit</text:h>
1427+<text:h text:style-name="Heading_20_1" text:outline-level="1"><text:bookmark-start text:name="links" />Links<text:bookmark-end text:name="links" /></text:h>
1428+<text:h text:style-name="Heading_20_2" text:outline-level="2"><text:bookmark-start text:name="explicit" />Explicit<text:bookmark-end text:name="explicit" /></text:h>
14201429<text:p text:style-name="First_20_paragraph">Just a
14211430<text:a xlink:type="simple" xlink:href="/url/" office:name=""><text:span text:style-name="Definition">URL</text:span></text:a>.</text:p>
14221431<text:p text:style-name="Text_20_body"><text:a xlink:type="simple" xlink:href="/url/" office:name="title"><text:span text:style-name="Definition">URL
@@ -1433,7 +1442,7 @@ and title</text:span></text:a></text:p>
14331442<text:p text:style-name="Text_20_body"><text:a xlink:type="simple" xlink:href="mailto:nobody@nowhere.net" office:name=""><text:span text:style-name="Definition">Email
14341443link</text:span></text:a></text:p>
14351444<text:p text:style-name="Text_20_body"><text:a xlink:type="simple" xlink:href="" office:name=""><text:span text:style-name="Definition">Empty</text:span></text:a>.</text:p>
1436−<text:h text:style-name="Heading_20_2" text:outline-level="2">Reference</text:h>
1445+<text:h text:style-name="Heading_20_2" text:outline-level="2"><text:bookmark-start text:name="reference" />Reference<text:bookmark-end text:name="reference" /></text:h>
14371446<text:p text:style-name="First_20_paragraph">Foo
14381447<text:a xlink:type="simple" xlink:href="/url/" office:name=""><text:span text:style-name="Definition">bar</text:span></text:a>.</text:p>
14391448<text:p text:style-name="Text_20_body">With
@@ -1453,8 +1462,8 @@ by itself should be a link.</text:p>
14531462<text:a xlink:type="simple" xlink:href="/url/" office:name="Title with &quot;quotes&quot; inside"><text:span text:style-name="Definition">bar</text:span></text:a>.</text:p>
14541463<text:p text:style-name="Text_20_body">Foo
14551464<text:a xlink:type="simple" xlink:href="/url/" office:name="Title with &quot;quote&quot; inside"><text:span text:style-name="Definition">biz</text:span></text:a>.</text:p>
1456−<text:h text:style-name="Heading_20_2" text:outline-level="2">With
1457−ampersands</text:h>
1465+<text:h text:style-name="Heading_20_2" text:outline-level="2"><text:bookmark-start text:name="with-ampersands" />With
1466+ampersands<text:bookmark-end text:name="with-ampersands" /></text:h>
14581467<text:p text:style-name="First_20_paragraph">Here’s a
14591468<text:a xlink:type="simple" xlink:href="http://example.com/?foo=1&amp;bar=2" office:name=""><text:span text:style-name="Definition">link
14601469with an ampersand in the URL</text:span></text:a>.</text:p>
@@ -1467,7 +1476,7 @@ link</text:span></text:a>.</text:p>
14671476<text:p text:style-name="Text_20_body">Here’s an
14681477<text:a xlink:type="simple" xlink:href="/script?foo=1&amp;bar=2" office:name=""><text:span text:style-name="Definition">inline
14691478link in pointy braces</text:span></text:a>.</text:p>
1470−<text:h text:style-name="Heading_20_2" text:outline-level="2">Autolinks</text:h>
1479+<text:h text:style-name="Heading_20_2" text:outline-level="2"><text:bookmark-start text:name="autolinks" />Autolinks<text:bookmark-end text:name="autolinks" /></text:h>
14711480<text:p text:style-name="First_20_paragraph">With an ampersand:
14721481<text:a xlink:type="simple" xlink:href="http://example.com/?foo=1&amp;bar=2" office:name=""><text:span text:style-name="Definition">http://example.com/?foo=1&amp;bar=2</text:span></text:a></text:p>
14731482<text:list text:style-name="L29">
@@ -1489,7 +1498,7 @@ link in pointy braces</text:span></text:a>.</text:p>
14891498<text:span text:style-name="Source_Text">&lt;http://example.com/&gt;</text:span></text:p>
14901499<text:p text:style-name="P57">or here: &lt;http://example.com/&gt;</text:p>
14911500<text:p text:style-name="Horizontal_20_Line" />
1492−<text:h text:style-name="Heading_20_1" text:outline-level="1">Images</text:h>
1501+<text:h text:style-name="Heading_20_1" text:outline-level="1"><text:bookmark-start text:name="images" />Images<text:bookmark-end text:name="images" /></text:h>
14931502<text:p text:style-name="First_20_paragraph">From “Voyage dans la Lune” by
14941503Georges Melies (1902):</text:p>
14951504<text:p text:style-name="FigureWithCaption"><draw:frame draw:name="img1"><draw:image xlink:href="lalune.jpg" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad" /></draw:frame></text:p>
@@ -1498,7 +1507,7 @@ Georges Melies (1902):</text:p>
14981507<draw:frame draw:name="img2"><draw:image xlink:href="movie.jpg" xlink:type="simple" xlink:show="embed" xlink:actuate="onLoad" /></draw:frame>
14991508icon.</text:p>
15001509<text:p text:style-name="Horizontal_20_Line" />
1501−<text:h text:style-name="Heading_20_1" text:outline-level="1">Footnotes</text:h>
1510+<text:h text:style-name="Heading_20_1" text:outline-level="1"><text:bookmark-start text:name="footnotes" />Footnotes<text:bookmark-end text:name="footnotes" /></text:h>
15021511<text:p text:style-name="First_20_paragraph">Here is a footnote
15031512reference,<text:note text:id="ftn0" text:note-class="footnote"><text:note-citation>1</text:note-citation><text:note-body><text:p text:style-name="Footnote">Here
15041513is the footnote. It can go anywhere after the footnote reference. It need not