Skip to content

Commit 970c136

Browse files
Docx reader: fix handling of empty fields
Some fields only have an instrText and no content, Pandoc didn't understand these, causing other fields to be misunderstood because it seemed like a field was still open when it wasn't.
1 parent 024c613 commit 970c136

File tree

4 files changed

+15
-0
lines changed

4 files changed

+15
-0
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,10 @@ elemToParPart ns element
855855
FldCharFieldInfo info : ancestors | fldCharType == "separate" -> do
856856
modify $ \st -> st {stateFldCharState = FldCharContent info [] : ancestors}
857857
return NullParPart
858+
-- Some fields have no content, since Pandoc doesn't understand any of those fields, we can just close it.
859+
FldCharFieldInfo _ : ancestors | fldCharType == "end" -> do
860+
modify $ \st -> st {stateFldCharState = ancestors}
861+
return NullParPart
858862
[FldCharContent info children] | fldCharType == "end" -> do
859863
modify $ \st -> st {stateFldCharState = []}
860864
return $ Field info $ reverse children

test/Tests/Readers/Docx.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ tests = [ testGroup "document"
151151
"nested fields with <w:instrText> tag"
152152
"docx/nested_instrText.docx"
153153
"docx/nested_instrText.native"
154+
, testCompare
155+
"empty fields with <w:instrText> tag"
156+
"docx/empty_field.docx"
157+
"docx/empty_field.native"
154158
, testCompare
155159
"pageref hyperlinks in <w:instrText> tag"
156160
"docx/pageref.docx"

test/docx/empty_field.docx

14 KB
Binary file not shown.

test/docx/empty_field.native

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[Para
2+
[Str "\24076\26395\28145\20837\20102\35299\30340\35835\32773\21487\20197\21435\30475David",Space,Str "French",Space,Str "Belding\21644Kevin",Space,Str "J.",Space,Str "Mitchell\30340"
3+
,Link ("",[],[]) [Str "Foundations",Space,Str "of",Space,Str "Analysis,",Space,Str "2nd",Space,Str "Edition"] ("https://books.google.com/books?id=sp_Zcb9ot90C&lpg=PR4&hl=zh-CN&pg=PA19#v=onepage&q&f=true",""),Str ",\21487\20174\&19\39029\30475\36215\65292\25110D.C.",Space,Str "Goldrei\30340",Space
4+
,Link ("",[],[]) [Str "Classic",Space,Str "Set",Space,Str "Theory:",Space,Str "For",Space,Str "Guided",Space,Str "Independent",Space,Str "Study"] ("https://books.google.ae/books?id=dlc0DwAAQBAJ&lpg=PT29&hl=zh-CN&pg=PT26#v=onepage&q&f=true","")
5+
,Str "\65292\20174\31532\20108\31456\30475\36215\65292\38405\35835\26102\35201\27880\24847\26412\25991\19982\36825\20123\20070\25152\19981\21516\30340\26159\24182\27809\26377\25226\23454\25968\30475\20316\26159\26377\29702\25968\38598\30340\20998\21106\12290"]
6+
,Para [Str "Index:"]
7+
,Para [Str "French,",Space,Str "1"]]

0 commit comments

Comments
 (0)