git.iko.soy/typeable/pandoc
on master help sign in
Texinfo writer: Fixed internal cross-references.

Now we insert anchors after each header, and use @ref instead of @uref for links. Commas are now escaped as @comma{} only when needed; previously all commas were escaped. (This change is needed, in part, because @ref commands must be followed by a real comma or period.) Also insert a blank line in from of @verbatim environments.

commit4ab30f3ceaec427b6f89f2fe32cef4b0b1b5e955 authorJohn MacFarlane <jgm@berkeley.edu> dateMon 24 Sep 2012, 19:13 (14 years ago) parent12045d8
2 files changed+93−33
src/Text/Pandoc/Writers/Texinfo.hs+26−7 tests/writer.texinfo+67−26
src/Text/Pandoc/Writers/Texinfo.hs+26−7
@@ -45,6 +45,8 @@ data WriterState =
4545 WriterState { stStrikeout :: Bool -- document contains strikeout
4646 , stSuperscript :: Bool -- document contains superscript
4747 , stSubscript :: Bool -- document contains subscript
48+ , stEscapeComma :: Bool -- in a context where we need @comma
49+ , stIdentifiers :: [String] -- header ids used already
4850 }
4951
5052{- TODO:
@@ -56,7 +58,8 @@ data WriterState =
5658writeTexinfo :: WriterOptions -> Pandoc -> String
5759writeTexinfo options document =
5860 evalState (pandocToTexinfo options $ wrapTop document) $
59− WriterState { stStrikeout = False, stSuperscript = False, stSubscript = False }
61+ WriterState { stStrikeout = False, stSuperscript = False,
62+ stEscapeComma = False, stSubscript = False, stIdentifiers = [] }
6063
6164-- | Add a "Top" node around the document, needed by Texinfo.
6265wrapTop :: Pandoc -> Pandoc
@@ -95,7 +98,6 @@ stringToTexinfo = escapeStringUsing texinfoEscapes
9598 where texinfoEscapes = [ ('{', "@{")
9699 , ('}', "@}")
97100 , ('@', "@@")
98− , (',', "@comma{}") -- only needed in argument lists
99101 , ('\160', "@ ")
100102 , ('\x2014', "---")
101103 , ('\x2013', "--")
@@ -103,6 +105,14 @@ stringToTexinfo = escapeStringUsing texinfoEscapes
103105 , ('\x2019', "'")
104106 ]
105107
108+escapeCommas :: State WriterState Doc -> State WriterState Doc
109+escapeCommas parser = do
110+ oldEscapeComma <- gets stEscapeComma
111+ modify $ \st -> st{ stEscapeComma = True }
112+ res <- parser
113+ modify $ \st -> st{ stEscapeComma = oldEscapeComma }
114+ return res
115+
106116-- | Puts contents into Texinfo command.
107117inCmd :: String -> Doc -> Doc
108118inCmd cmd contents = char '@' <> text cmd <> braces contents
@@ -134,7 +144,8 @@ blockToTexinfo (BlockQuote lst) = do
134144 text "@end quotation"
135145
136146blockToTexinfo (CodeBlock _ str) = do
137− return $ text "@verbatim" $$
147+ return $ blankline $$
148+ text "@verbatim" $$
138149 flush (text str) $$
139150 text "@end verbatim" <> blankline
140151
@@ -194,9 +205,13 @@ blockToTexinfo (Header 0 lst) = do
194205blockToTexinfo (Header level lst) = do
195206 node <- inlineListForNode lst
196207 txt <- inlineListToTexinfo lst
208+ idsUsed <- gets stIdentifiers
209+ let id' = uniqueIdent lst idsUsed
210+ modify $ \st -> st{ stIdentifiers = id' : idsUsed }
197211 return $ if (level > 0) && (level <= 4)
198− then blankline <> text "@node " <> node <> cr <>
199− text (seccmd level) <> txt
212+ then blankline <> text "@node " <> node $$
213+ text (seccmd level) <> txt $$
214+ text "@anchor" <> braces (text $ '#':id')
200215 else txt
201216 where
202217 seccmd 1 = "@chapter "
@@ -404,17 +419,21 @@ inlineToTexinfo (RawInline _ _) = return empty
404419inlineToTexinfo (LineBreak) = return $ text "@*"
405420inlineToTexinfo Space = return $ char ' '
406421
422+inlineToTexinfo (Link txt (src@('#':_), _)) = do
423+ contents <- escapeCommas $ inlineListToTexinfo txt
424+ return $ text "@ref" <>
425+ braces (text (stringToTexinfo src) <> text "," <> contents)
407426inlineToTexinfo (Link txt (src, _)) = do
408427 case txt of
409428 [Code _ x] | x == src -> -- autolink
410429 do return $ text $ "@url{" ++ x ++ "}"
411− _ -> do contents <- inlineListToTexinfo txt
430+ _ -> do contents <- escapeCommas $ inlineListToTexinfo txt
412431 let src1 = stringToTexinfo src
413432 return $ text ("@uref{" ++ src1 ++ ",") <> contents <>
414433 char '}'
415434
416435inlineToTexinfo (Image alternate (source, _)) = do
417− content <- inlineListToTexinfo alternate
436+ content <- escapeCommas $ inlineListToTexinfo alternate
418437 return $ text ("@image{" ++ base ++ ",,,") <> content <> text "," <>
419438 text (ext ++ "}")
420439 where
tests/writer.texinfo+67−26
@@ -30,7 +30,7 @@ _@{\text\@}
3030@title Pandoc Test Suite
3131@author John MacFarlane
3232@author Anonymous
33−July 17@comma{} 2006
33+July 17, 2006
3434@end titlepage
3535
3636@node Top
@@ -64,28 +64,33 @@ This is a set of tests for pandoc. Most of them are adapted from John Gruber's m
6464
6565@node Headers
6666@chapter Headers
67+@anchor{#headers}
6768@menu
6869* Level 2 with an embedded link::
6970@end menu
7071
7172@node Level 2 with an embedded link
7273@section Level 2 with an @uref{/url,embedded link}
74+@anchor{#level-2-with-an-embedded-link}
7375@menu
7476* Level 3 with emphasis::
7577@end menu
7678
7779@node Level 3 with emphasis
7880@subsection Level 3 with @emph{emphasis}
81+@anchor{#level-3-with-emphasis}
7982@menu
8083* Level 4::
8184@end menu
8285
8386@node Level 4
8487@subsubsection Level 4
88+@anchor{#level-4}
8589Level 5
8690
8791@node Level 1
8892@chapter Level 1
93+@anchor{#level-1}
8994@menu
9095* Level 2 with emphasis::
9196* Level 2::
@@ -93,16 +98,19 @@ Level 5
9398
9499@node Level 2 with emphasis
95100@section Level 2 with @emph{emphasis}
101+@anchor{#level-2-with-emphasis}
96102@menu
97103* Level 3::
98104@end menu
99105
100106@node Level 3
101107@subsection Level 3
108+@anchor{#level-3}
102109with no blank line
103110
104111@node Level 2
105112@section Level 2
113+@anchor{#level-2}
106114with no blank line
107115
108116@iftex
@@ -114,6 +122,7 @@ with no blank line
114122
115123@node Paragraphs
116124@chapter Paragraphs
125+@anchor{#paragraphs}
117126Here's a regular paragraph.
118127
119128In Markdown 1.0.0 and earlier. Version 8. This line turns into a list item. Because a hard-wrapped line in the middle of a paragraph looked like a list item.
@@ -131,6 +140,7 @@ There should be a hard line break@*here.
131140
132141@node Block Quotes
133142@chapter Block Quotes
143+@anchor{#block-quotes}
134144E-mail style:
135145
136146@quotation
@@ -138,6 +148,7 @@ This is a block quote. It is pretty short.
138148@end quotation
139149@quotation
140150Code in a block quote:
151+
141152@verbatim
142153sub status {
143154 print "working";
@@ -175,7 +186,9 @@ And a following paragraph.
175186
176187@node Code Blocks
177188@chapter Code Blocks
189+@anchor{#code-blocks}
178190Code:
191+
179192@verbatim
180193---- (should be four hyphens)
181194
@@ -187,6 +200,7 @@ this code block is indented by one tab
187200@end verbatim
188201
189202And:
203+
190204@verbatim
191205 this code block is indented by two tabs
192206
@@ -202,6 +216,7 @@ These should not be escaped: \$ \\ \> \[ \{
202216
203217@node Lists
204218@chapter Lists
219+@anchor{#lists}
205220@menu
206221* Unordered::
207222* Ordered::
@@ -212,6 +227,7 @@ These should not be escaped: \$ \\ \> \[ \{
212227
213228@node Unordered
214229@section Unordered
230+@anchor{#unordered}
215231Asterisks tight:
216232
217233@itemize
@@ -289,6 +305,7 @@ Minus 3
289305
290306@node Ordered
291307@section Ordered
308+@anchor{#ordered}
292309Tight:
293310
294311@enumerate
@@ -343,7 +360,7 @@ Multiple paragraphs:
343360
344361@enumerate
345362@item
346−Item 1@comma{} graf one.
363+Item 1, graf one.
347364
348365Item 1. graf two. The quick brown fox jumped over the lazy dog's back.
349366
@@ -357,6 +374,7 @@ Item 3.
357374
358375@node Nested
359376@section Nested
377+@anchor{#nested}
360378@itemize
361379@item
362380Tab
@@ -417,6 +435,7 @@ Third
417435
418436@node Tabs and spaces
419437@section Tabs and spaces
438+@anchor{#tabs-and-spaces}
420439@itemize
421440@item
422441this is a list item indented with tabs
@@ -437,6 +456,7 @@ this is an example list item indented with spaces
437456
438457@node Fancy list markers
439458@section Fancy list markers
459+@anchor{#fancy-list-markers}
440460@enumerate 2
441461@item
442462begins with 2
@@ -447,7 +467,7 @@ with a continuation
447467
448468@enumerate 4
449469@item
450−sublist with roman numerals@comma{} starting with 4
470+sublist with roman numerals, starting with 4
451471@item
452472more items
453473@enumerate A
@@ -512,6 +532,7 @@ B. Williams
512532
513533@node Definition Lists
514534@chapter Definition Lists
535+@anchor{#definition-lists}
515536Tight using spaces:
516537
517538@table @asis
@@ -564,11 +585,12 @@ Multiple blocks with italics:
564585
565586red fruit
566587
567−contains seeds@comma{} crisp@comma{} pleasant to taste
588+contains seeds, crisp, pleasant to taste
568589
569590@item @emph{orange}
570591
571592orange fruit
593+
572594@verbatim
573595{ orange code block }
574596@end verbatim
@@ -578,7 +600,7 @@ orange block quote
578600@end quotation
579601@end table
580602
581−Multiple definitions@comma{} tight:
603+Multiple definitions, tight:
582604
583605@table @asis
584606@item apple
@@ -591,7 +613,7 @@ orange fruit
591613bank
592614@end table
593615
594−Multiple definitions@comma{} loose:
616+Multiple definitions, loose:
595617
596618@table @asis
597619@item apple
@@ -608,7 +630,7 @@ bank
608630
609631@end table
610632
611−Blank line after term@comma{} indented marker@comma{} alternate markers:
633+Blank line after term, indented marker, alternate markers:
612634
613635@table @asis
614636@item apple
@@ -632,6 +654,7 @@ sublist
632654
633655@node HTML Blocks
634656@chapter HTML Blocks
657+@anchor{#html-blocks}
635658Simple block on one line:
636659
637660foo
@@ -646,7 +669,8 @@ And this is @strong{strong}
646669Here's a simple block:
647670
648671foo
649−This should be a code block@comma{} though:
672+This should be a code block, though:
673+
650674@verbatim
651675<div>
652676 foo
@@ -654,11 +678,12 @@ This should be a code block@comma{} though:
654678@end verbatim
655679
656680As should this:
681+
657682@verbatim
658683<div>foo</div>
659684@end verbatim
660685
661−Now@comma{} nested:
686+Now, nested:
662687
663688foo
664689This should just be an HTML comment:
@@ -666,13 +691,15 @@ This should just be an HTML comment:
666691Multiline:
667692
668693Code block:
694+
669695@verbatim
670696<!-- Comment -->
671697@end verbatim
672698
673−Just plain comment@comma{} with trailing spaces on the line:
699+Just plain comment, with trailing spaces on the line:
674700
675701Code:
702+
676703@verbatim
677704<hr />
678705@end verbatim
@@ -688,9 +715,10 @@ Hr's:
688715
689716@node Inline Markup
690717@chapter Inline Markup
691−This is @emph{emphasized}@comma{} and so @emph{is this}.
718+@anchor{#inline-markup}
719+This is @emph{emphasized}, and so @emph{is this}.
692720
693−This is @strong{strong}@comma{} and so @strong{is this}.
721+This is @strong{strong}, and so @strong{is this}.
694722
695723An @emph{@uref{/url,emphasized link}}.
696724
@@ -702,15 +730,15 @@ So is @strong{@emph{this}} word.
702730
703731So is @strong{@emph{this}} word.
704732
705−This is code: @code{>}@comma{} @code{$}@comma{} @code{\}@comma{} @code{\$}@comma{} @code{<html>}.
733+This is code: @code{>}, @code{$}, @code{\}, @code{\$}, @code{<html>}.
706734
707735@textstrikeout{This is @emph{strikeout}.}
708736
709737Superscripts: a@textsuperscript{bc}d a@textsuperscript{@emph{hello}} a@textsuperscript{hello@ there}.
710738
711−Subscripts: H@textsubscript{2}O@comma{} H@textsubscript{23}O@comma{} H@textsubscript{many@ of@ them}O.
739+Subscripts: H@textsubscript{2}O, H@textsubscript{23}O, H@textsubscript{many@ of@ them}O.
712740
713−These should not be superscripts or subscripts@comma{} because of the unescaped spaces: a^b c^d@comma{} a~b c~d.
741+These should not be superscripts or subscripts, because of the unescaped spaces: a^b c^d, a~b c~d.
714742
715743@iftex
716744@bigskip@hrule@bigskip
@@ -720,20 +748,21 @@ These should not be superscripts or subscripts@comma{} because of the unescaped
720748@end ifnottex
721749
722750@node Smart quotes ellipses dashes
723−@chapter Smart quotes@comma{} ellipses@comma{} dashes
724−``Hello@comma{}'' said the spider. ```Shelob' is my name.''
751+@chapter Smart quotes, ellipses, dashes
752+@anchor{#smart-quotes-ellipses-dashes}
753+``Hello,'' said the spider. ```Shelob' is my name.''
725754
726−`A'@comma{} `B'@comma{} and `C' are letters.
755+`A', `B', and `C' are letters.
727756
728−`Oak@comma{}' `elm@comma{}' and `beech' are names of trees. So is `pine.'
757+`Oak,' `elm,' and `beech' are names of trees. So is `pine.'
729758
730−`He said@comma{} ``I want to go.''' Were you alive in the 70's?
759+`He said, ``I want to go.''' Were you alive in the 70's?
731760
732761Here is some quoted `@code{code}' and a ``@uref{http://example.com/?foo=1&bar=2,quoted link}''.
733762
734763Some dashes: one---two --- three---four --- five.
735764
736−Dashes between numbers: 5--7@comma{} 255--66@comma{} 1987--1999.
765+Dashes between numbers: 5--7, 255--66, 1987--1999.
737766
738767Ellipses@dots{}and@dots{}and@dots{}.
739768
@@ -746,6 +775,7 @@ Ellipses@dots{}and@dots{}and@dots{}.
746775
747776@node LaTeX
748777@chapter LaTeX
778+@anchor{#latex}
749779@itemize
750780@item
751781@tex
@@ -771,9 +801,9 @@ These shouldn't be math:
771801
772802@itemize
773803@item
774−To get the famous equation@comma{} write @code{$e = mc^2$}.
804+To get the famous equation, write @code{$e = mc^2$}.
775805@item
776−$22@comma{}000 is a @emph{lot} of money. So is $34@comma{}000. (It worked if ``lot'' is emphasized.)
806+$22,000 is a @emph{lot} of money. So is $34,000. (It worked if ``lot'' is emphasized.)
777807@item
778808Shoes ($20) and socks ($5).
779809@item
@@ -798,6 +828,7 @@ Cat & 1 \\ \hline
798828
799829@node Special Characters
800830@chapter Special Characters
831+@anchor{#special-characters}
801832Here is some unicode:
802833
803834@itemize
@@ -864,6 +895,7 @@ Minus: -
864895
865896@node Links
866897@chapter Links
898+@anchor{#links}
867899@menu
868900* Explicit::
869901* Reference::
@@ -873,6 +905,7 @@ Minus: -
873905
874906@node Explicit
875907@section Explicit
908+@anchor{#explicit}
876909Just a @uref{/url/,URL}.
877910
878911@uref{/url/,URL and title}.
@@ -893,6 +926,7 @@ Just a @uref{/url/,URL}.
893926
894927@node Reference
895928@section Reference
929+@anchor{#reference}
896930Foo @uref{/url/,bar}.
897931
898932Foo @uref{/url/,bar}.
@@ -910,6 +944,7 @@ Indented @uref{/url,twice}.
910944Indented @uref{/url,thrice}.
911945
912946This should [not][] be a link.
947+
913948@verbatim
914949[not]: /url
915950@end verbatim
@@ -920,6 +955,7 @@ Foo @uref{/url/,biz}.
920955
921956@node With ampersands
922957@section With ampersands
958+@anchor{#with-ampersands}
923959Here's a @uref{http://example.com/?foo=1&bar=2,link with an ampersand in the URL}.
924960
925961Here's a link with an amersand in the link text: @uref{http://att.com/,AT&T}.
@@ -930,6 +966,7 @@ Here's an @uref{/script?foo=1&bar=2,inline link in pointy braces}.
930966
931967@node Autolinks
932968@section Autolinks
969+@anchor{#autolinks}
933970With an ampersand: @url{http://example.com/?foo=1&bar=2}
934971
935972@itemize
@@ -947,6 +984,7 @@ An e-mail address: @uref{mailto:nobody@@nowhere.net,@code{nobody@@nowhere.net}}
947984Blockquoted: @url{http://example.com/}
948985@end quotation
949986Auto-links should not occur here: @code{<http://example.com/>}
987+
950988@verbatim
951989or here: <http://example.com/>
952990@end verbatim
@@ -960,6 +998,7 @@ or here: <http://example.com/>
960998
961999@node Images
9621000@chapter Images
1001+@anchor{#images}
9631002From ``Voyage dans la Lune'' by Georges Melies (1902):
9641003
9651004@float
@@ -978,14 +1017,16 @@ Here is a movie @image{movie,,,movie,jpg} icon.
9781017
9791018@node Footnotes
9801019@chapter Footnotes
981−Here is a footnote reference@comma{}@footnote{Here is the footnote. It can go anywhere after the footnote reference. It need not be placed at the end of the document.} and another.@footnote{Here's the long note. This one contains multiple blocks.
1020+@anchor{#footnotes}
1021+Here is a footnote reference,@footnote{Here is the footnote. It can go anywhere after the footnote reference. It need not be placed at the end of the document.} and another.@footnote{Here's the long note. This one contains multiple blocks.
9821022
9831023Subsequent blocks are indented to show that they belong to the footnote (as with list items).
1024+
9841025@verbatim
9851026 { <code> }
9861027@end verbatim
9871028
988−If you want@comma{} you can indent every line@comma{} but you can also be lazy and just indent the first line of each block.} This should @emph{not} be a footnote reference@comma{} because it contains a space.[^my note] Here is an inline note.@footnote{This is @emph{easier} to type. Inline notes may contain @uref{http://google.com,links} and @code{]} verbatim characters@comma{} as well as [bracketed text].}
1029+If you want, you can indent every line, but you can also be lazy and just indent the first line of each block.} This should @emph{not} be a footnote reference, because it contains a space.[^my note] Here is an inline note.@footnote{This is @emph{easier} to type. Inline notes may contain @uref{http://google.com,links} and @code{]} verbatim characters, as well as [bracketed text].}
9891030
9901031@quotation
9911032Notes can go in quotes.@footnote{In quote.}
@@ -995,6 +1036,6 @@ Notes can go in quotes.@footnote{In quote.}
9951036And in list items.@footnote{In list.}
9961037@end enumerate
9971038
998−This paragraph should not be part of the note@comma{} as it is not indented.
1039+This paragraph should not be part of the note, as it is not indented.
9991040
10001041@bye