Skip to content

Commit

Permalink
Docx reader: fix handling of empty fields
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mbrackeantidot authored and Guillaume Chapuis committed Oct 1, 2021
1 parent 0b232b2 commit b9794e5
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Text/Pandoc/Readers/Docx/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -858,6 +858,10 @@ elemToParPart ns element
FldCharFieldInfo info : ancestors | fldCharType == "separate" -> do
modify $ \st -> st {stateFldCharState = FldCharContent info [] : ancestors}
return NullParPart
-- Some fields have no content, since Pandoc doesn't understand any of those fields, we can just close it.
FldCharFieldInfo _ : ancestors | fldCharType == "end" -> do
modify $ \st -> st {stateFldCharState = ancestors}
return NullParPart
[FldCharContent info children] | fldCharType == "end" -> do
modify $ \st -> st {stateFldCharState = []}
return $ Field info $ reverse children
Expand Down
4 changes: 4 additions & 0 deletions test/Tests/Readers/Docx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ tests = [ testGroup "document"
"nested fields with <w:instrText> tag"
"docx/nested_instrText.docx"
"docx/nested_instrText.native"
, testCompare
"empty fields with <w:instrText> tag"
"docx/empty_field.docx"
"docx/empty_field.native"
, testCompare
"pageref hyperlinks in <w:instrText> tag"
"docx/pageref.docx"
Expand Down
Binary file added test/docx/empty_field.docx
Binary file not shown.
7 changes: 7 additions & 0 deletions test/docx/empty_field.native
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[Para
[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"
,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
,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","")
,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"]
,Para [Str "Index:"]
,Para [Str "French,",Space,Str "1"]]

0 comments on commit b9794e5

Please sign in to comment.