LaTeX reader: use Link instead of Span for \ref.

This makes more sense semantically and avoids unnecessary
Span [Link] nestings when references are resolved.
This commit is contained in:
John MacFarlane 2017-08-16 10:46:56 -07:00
parent c6ec189a96
commit db715ca847
2 changed files with 12 additions and 11 deletions

View file

@ -111,13 +111,12 @@ parseLaTeX = do
return $ Pandoc meta bs'
resolveRefs :: M.Map String [Inline] -> Inline -> Inline
resolveRefs labels x@(Span (ident,classes,kvs) _) =
resolveRefs labels x@(Link (ident,classes,kvs) _ _) =
case (lookup "reference-type" kvs,
lookup "reference" kvs) of
(Just "ref", Just lab) ->
case M.lookup lab labels of
Just txt -> Span (ident,classes,kvs)
[Link nullAttr txt ('#':lab, "")]
Just txt -> Link (ident,classes,kvs) txt ('#':lab, "")
Nothing -> x
_ -> x
resolveRefs _ x = x
@ -1538,9 +1537,11 @@ doref :: PandocMonad m => String -> LP m Inlines
doref cls = do
v <- braced
let refstr = toksToString v
return $ spanWith ("",[],[ ("reference-type", cls)
return $ linkWith ("",[],[ ("reference-type", cls)
, ("reference", refstr)])
$ inBrackets $ str refstr
('#':refstr)
""
(inBrackets $ str refstr)
lookupListDefault :: (Show k, Ord k) => v -> [k] -> M.Map k v -> v
lookupListDefault d = (fromMaybe d .) . lookupList

View file

@ -2,21 +2,21 @@
% pandoc -f latex -t native
Figure \ref{fig:1}
^D
[Para [Str "Figure",Space,Span ("",[],[("reference-type","ref"),("reference","fig:1")]) [Str "[fig:1]"]]]
[Para [Str "Figure",Space,Link ("",[],[("reference-type","ref"),("reference","fig:1")]) [Str "[fig:1]"] ("#fig:1","")]]
```
```
% pandoc -f latex -t native
Figure \cref{fig:1}
^D
[Para [Str "Figure",Space,Span ("",[],[("reference-type","ref"),("reference","fig:1")]) [Str "[fig:1]"]]]
[Para [Str "Figure",Space,Link ("",[],[("reference-type","ref"),("reference","fig:1")]) [Str "[fig:1]"] ("#fig:1","")]]
```
```
% pandoc -f latex -t native
Figure \vref{fig:1}
^D
[Para [Str "Figure",Space,Span ("",[],[("reference-type","ref+page"),("reference","fig:1")]) [Str "[fig:1]"]]]
[Para [Str "Figure",Space,Link ("",[],[("reference-type","ref+page"),("reference","fig:1")]) [Str "[fig:1]"] ("#fig:1","")]]
```
```
@ -28,7 +28,7 @@ Accuracy~\eqref{eq:Accuracy} is the proportion, measuring true results among all
Accuracy = \frac{t_p + t_n}{t_p + f_p + f_n + t_n}
\end{equation}
^D
[Para [Str "Accuracy\160",Span ("",[],[("reference-type","eqref"),("reference","eq:Accuracy")]) [Str "[eq:Accuracy]"],Space,Str "is",Space,Str "the",Space,Str "proportion,",Space,Str "measuring",Space,Str "true",Space,Str "results",Space,Str "among",Space,Str "all",Space,Str "results."]
[Para [Str "Accuracy\160",Link ("",[],[("reference-type","eqref"),("reference","eq:Accuracy")]) [Str "[eq:Accuracy]"] ("#eq:Accuracy",""),Space,Str "is",Space,Str "the",Space,Str "proportion,",Space,Str "measuring",Space,Str "true",Space,Str "results",Space,Str "among",Space,Str "all",Space,Str "results."]
,Para [Math DisplayMath "\\label{eq:Accuracy}\n Accuracy = \\frac{t_p + t_n}{t_p + f_p + f_n + t_n}"]]
```
@ -43,12 +43,12 @@ Accuracy~\eqref{eq:Accuracy} is the proportion, measuring true results among all
Figure \ref{fig:Logo} illustrated the SVG logo
^D
[Para [Image ("",[],[]) [Str "Logo",Span ("",[],[("label","fig:Logo")]) []] ("command/SVG_logo.svg","fig:")]
,Para [Str "Figure",Space,Span ("",[],[("reference-type","ref"),("reference","fig:Logo")]) [Str "[fig:Logo]"],Space,Str "illustrated",Space,Str "the",Space,Str "SVG",Space,Str "logo"]]
,Para [Str "Figure",Space,Link ("",[],[("reference-type","ref"),("reference","fig:Logo")]) [Str "[fig:Logo]"] ("#fig:Logo",""),Space,Str "illustrated",Space,Str "the",Space,Str "SVG",Space,Str "logo"]]
```
```
% pandoc -f latex -t native
\label{section} Section \ref{section}
^D
[Para [Span ("",[],[("label","section")]) [Str "[section]"],Space,Str "Section",Space,Span ("",[],[("reference-type","ref"),("reference","section")]) [Str "[section]"]]]
[Para [Span ("",[],[("label","section")]) [Str "[section]"],Space,Str "Section",Space,Link ("",[],[("reference-type","ref"),("reference","section")]) [Str "[section]"] ("#section","")]]
```