aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEzwal <[email protected]>2025-11-30 13:49:58 +0100
committerGitHub <[email protected]>2025-11-30 13:49:58 +0100
commitfc07b06851eac5c70557de101d8044494e9a2bc4 (patch)
tree8146fbecf7dbcefc0a707c0102e53cbdafbecafd
parent094061d946f12961b9af0b400befbb798f980be7 (diff)
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...
-rw-r--r--src/Text/Pandoc/Readers/Docx.hs1
-rw-r--r--src/Text/Pandoc/Readers/Docx/Fields.hs12
-rw-r--r--test/command/9002.md2
-rw-r--r--test/docx/cross_reference.docxbin0 -> 14978 bytes
-rw-r--r--test/docx/cross_reference.native7
-rw-r--r--test/docx/table_captions_with_field.native40
6 files changed, 56 insertions, 6 deletions
diff --git a/src/Text/Pandoc/Readers/Docx.hs b/src/Text/Pandoc/Readers/Docx.hs
index 2852defc7..8f70fe7a6 100644
--- a/src/Text/Pandoc/Readers/Docx.hs
+++ b/src/Text/Pandoc/Readers/Docx.hs
@@ -482,6 +482,7 @@ parPartToInlines' (Field info children) =
++ [("bold","") | entryBold ie]
++ [("italic","") | entryItalic ie])) mempty
PagerefField fieldAnchor True -> parPartToInlines' $ InternalHyperLink fieldAnchor children
+ CrossrefField fieldAnchor True -> parPartToInlines' $ InternalHyperLink fieldAnchor children
EndNoteCite t -> do
formattedCite <- smushInlines <$> mapM parPartToInlines' children
opts <- asks docxOptions
diff --git a/src/Text/Pandoc/Readers/Docx/Fields.hs b/src/Text/Pandoc/Readers/Docx/Fields.hs
index 8b1837394..89bba8814 100644
--- a/src/Text/Pandoc/Readers/Docx/Fields.hs
+++ b/src/Text/Pandoc/Readers/Docx/Fields.hs
@@ -35,6 +35,7 @@ data IndexEntry = IndexEntry
data FieldInfo = HyperlinkField URL
-- The boolean indicates whether the field is a hyperlink.
| PagerefField Anchor Bool
+ | CrossrefField Anchor Bool
| IndexrefField IndexEntry
| CslCitation T.Text
| CslBibliography
@@ -57,6 +58,8 @@ fieldInfo = do
<|>
indexref
<|>
+ crossref
+ <|>
addIn
<|>
return UnknownField
@@ -141,6 +144,15 @@ pageref = do
let isLink = any ((== 'h') . fst) switches
return $ PagerefField farg isLink
+crossref :: Parser FieldInfo
+crossref = do
+ string "REF"
+ spaces
+ farg <- fieldArgument
+ switches <- many fieldSwitch
+ let isLink = any ((== 'h') . fst) switches
+ return $ CrossrefField farg isLink
+
-- second element of tuple is optional "see".
indexref :: Parser FieldInfo
indexref = do
diff --git a/test/command/9002.md b/test/command/9002.md
index 91f983881..bac9d23bc 100644
--- a/test/command/9002.md
+++ b/test/command/9002.md
@@ -16,5 +16,5 @@
<tbody>
</tbody>
</table>
-<p>See Table 1 This is my table!</p>
+<p>See <a href="#_Ref143272803">Table 1 This is my table!</a></p>
```
diff --git a/test/docx/cross_reference.docx b/test/docx/cross_reference.docx
new file mode 100644
index 000000000..a0efe8386
--- /dev/null
+++ b/test/docx/cross_reference.docx
Binary files differ
diff --git a/test/docx/cross_reference.native b/test/docx/cross_reference.native
new file mode 100644
index 000000000..eb58cde2a
--- /dev/null
+++ b/test/docx/cross_reference.native
@@ -0,0 +1,7 @@
+[ Header 1 ( "title" , [] , [] ) [ Str "TITLE" ]
+, Para
+ [ Str "Cross-reference:"
+ , Space
+ , Link ( "" , [] , [] ) [ Str "TITLE" ] ( "#title" , "" )
+ ]
+]
diff --git a/test/docx/table_captions_with_field.native b/test/docx/table_captions_with_field.native
index 917e6071e..5fd802602 100644
--- a/test/docx/table_captions_with_field.native
+++ b/test/docx/table_captions_with_field.native
@@ -1,8 +1,23 @@
[ Para
- [ Str "See" , Space , Str "Table" , Space , Str "1." ]
+ [ Str "See"
+ , Space
+ , Link
+ ( "" , [] , [] )
+ [ Str "Table" , Space , Str "1" ]
+ ( "#_Ref71265628" , "" )
+ , Str "."
+ ]
, Table
( "" , [] , [] )
- (Caption Nothing [ Para [ Str "Table" , Space , Str "1" ] ])
+ (Caption
+ Nothing
+ [ Para
+ [ Span ( "_Ref71265628" , [ "anchor" ] , [] ) []
+ , Str "Table"
+ , Space
+ , Str "1"
+ ]
+ ])
[ ( AlignDefault , ColWidth 0.7605739372523824 )
, ( AlignDefault , ColWidth 0.11971303137380876 )
, ( AlignDefault , ColWidth 0.11971303137380876 )
@@ -79,7 +94,15 @@
, Header 2 ( "section" , [] , [] ) []
, Table
( "" , [] , [] )
- (Caption Nothing [ Para [ Str "Table" , Space , Str "2" ] ])
+ (Caption
+ Nothing
+ [ Para
+ [ Span ( "_Ref71265695" , [ "anchor" ] , [] ) []
+ , Str "Table"
+ , Space
+ , Str "2"
+ ]
+ ])
[ ( AlignDefault , ColWidth 0.33329636202307006 )
, ( AlignDefault , ColWidth 0.33329636202307006 )
, ( AlignDefault , ColWidth 0.33340727595385977 )
@@ -111,5 +134,12 @@
[ TableBody ( "" , [] , [] ) (RowHeadColumns 0) [] [] ]
(TableFoot ( "" , [] , [] ) [])
, Para
- [ Str "See" , Space , Str "Table" , Space , Str "2." ]
-] \ No newline at end of file
+ [ Str "See"
+ , Space
+ , Link
+ ( "" , [] , [] )
+ [ Str "Table" , Space , Str "2" ]
+ ( "#_Ref71265695" , "" )
+ , Str "."
+ ]
+]