Skip to content

Commit fc07b06

Browse files
authored
Docx reader: Handle REF link instruction (#11296)
This PR aims to handle a common run field instruction (fieldInstr) from docx format : REF, specifically those with the "link" switch \h. In word software, you can create REF field instruction with the Cross-reference button. You can create cross-reference to many things such as Equation, Table, Title...
1 parent 094061d commit fc07b06

File tree

6 files changed

+56
-6
lines changed

6 files changed

+56
-6
lines changed

src/Text/Pandoc/Readers/Docx.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ parPartToInlines' (Field info children) =
482482
++ [("bold","") | entryBold ie]
483483
++ [("italic","") | entryItalic ie])) mempty
484484
PagerefField fieldAnchor True -> parPartToInlines' $ InternalHyperLink fieldAnchor children
485+
CrossrefField fieldAnchor True -> parPartToInlines' $ InternalHyperLink fieldAnchor children
485486
EndNoteCite t -> do
486487
formattedCite <- smushInlines <$> mapM parPartToInlines' children
487488
opts <- asks docxOptions

src/Text/Pandoc/Readers/Docx/Fields.hs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ data IndexEntry = IndexEntry
3535
data FieldInfo = HyperlinkField URL
3636
-- The boolean indicates whether the field is a hyperlink.
3737
| PagerefField Anchor Bool
38+
| CrossrefField Anchor Bool
3839
| IndexrefField IndexEntry
3940
| CslCitation T.Text
4041
| CslBibliography
@@ -57,6 +58,8 @@ fieldInfo = do
5758
<|>
5859
indexref
5960
<|>
61+
crossref
62+
<|>
6063
addIn
6164
<|>
6265
return UnknownField
@@ -141,6 +144,15 @@ pageref = do
141144
let isLink = any ((== 'h') . fst) switches
142145
return $ PagerefField farg isLink
143146

147+
crossref :: Parser FieldInfo
148+
crossref = do
149+
string "REF"
150+
spaces
151+
farg <- fieldArgument
152+
switches <- many fieldSwitch
153+
let isLink = any ((== 'h') . fst) switches
154+
return $ CrossrefField farg isLink
155+
144156
-- second element of tuple is optional "see".
145157
indexref :: Parser FieldInfo
146158
indexref = do

test/command/9002.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
<tbody>
1717
</tbody>
1818
</table>
19-
<p>See Table 1 This is my table!</p>
19+
<p>See <a href="#_Ref143272803">Table 1 This is my table!</a></p>
2020
```

test/docx/cross_reference.docx

14.6 KB
Binary file not shown.

test/docx/cross_reference.native

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[ Header 1 ( "title" , [] , [] ) [ Str "TITLE" ]
2+
, Para
3+
[ Str "Cross-reference:"
4+
, Space
5+
, Link ( "" , [] , [] ) [ Str "TITLE" ] ( "#title" , "" )
6+
]
7+
]

test/docx/table_captions_with_field.native

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
[ Para
2-
[ Str "See" , Space , Str "Table" , Space , Str "1." ]
2+
[ Str "See"
3+
, Space
4+
, Link
5+
( "" , [] , [] )
6+
[ Str "Table" , Space , Str "1" ]
7+
( "#_Ref71265628" , "" )
8+
, Str "."
9+
]
310
, Table
411
( "" , [] , [] )
5-
(Caption Nothing [ Para [ Str "Table" , Space , Str "1" ] ])
12+
(Caption
13+
Nothing
14+
[ Para
15+
[ Span ( "_Ref71265628" , [ "anchor" ] , [] ) []
16+
, Str "Table"
17+
, Space
18+
, Str "1"
19+
]
20+
])
621
[ ( AlignDefault , ColWidth 0.7605739372523824 )
722
, ( AlignDefault , ColWidth 0.11971303137380876 )
823
, ( AlignDefault , ColWidth 0.11971303137380876 )
@@ -79,7 +94,15 @@
7994
, Header 2 ( "section" , [] , [] ) []
8095
, Table
8196
( "" , [] , [] )
82-
(Caption Nothing [ Para [ Str "Table" , Space , Str "2" ] ])
97+
(Caption
98+
Nothing
99+
[ Para
100+
[ Span ( "_Ref71265695" , [ "anchor" ] , [] ) []
101+
, Str "Table"
102+
, Space
103+
, Str "2"
104+
]
105+
])
83106
[ ( AlignDefault , ColWidth 0.33329636202307006 )
84107
, ( AlignDefault , ColWidth 0.33329636202307006 )
85108
, ( AlignDefault , ColWidth 0.33340727595385977 )
@@ -111,5 +134,12 @@
111134
[ TableBody ( "" , [] , [] ) (RowHeadColumns 0) [] [] ]
112135
(TableFoot ( "" , [] , [] ) [])
113136
, Para
114-
[ Str "See" , Space , Str "Table" , Space , Str "2." ]
115-
]
137+
[ Str "See"
138+
, Space
139+
, Link
140+
( "" , [] , [] )
141+
[ Str "Table" , Space , Str "2" ]
142+
( "#_Ref71265695" , "" )
143+
, Str "."
144+
]
145+
]

0 commit comments

Comments
 (0)