Skip to content

custom-style has offset while writing docx file #10748

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
gaiyinaizhi opened this issue Apr 2, 2025 · 3 comments
Open

custom-style has offset while writing docx file #10748

gaiyinaizhi opened this issue Apr 2, 2025 · 3 comments
Labels

Comments

@gaiyinaizhi
Copy link

Explain the problem.

  • command:
pandoc /Users/xx.html \
 -f html+tex_math_dollars --reference-doc /Users/xx/template.docx \
 --resource-path /Users/xx/output \
 -L /Users/xx/pagebreak.lua \
 -o /Users/xx.docx -s\
 --log /Users/xx.log
  • problem
    the custom-style has offset while convert html to docx,
    example html:
... skip outer html tag
<h2>hello sample h2 by default</h2>
<h2 custom-style="heading 2 center">hello sample h2 for center alignment</h2>
<p custom-style="Body Text center">body text for center alignment</p>
<div class="pagebreak page_0"></div> <!-- this is a pagebreak tag -->

example styles.xml in template.docx

	<w:style w:styleId="5" w:type="paragraph">
		<w:name w:val="heading 2"/>
		<w:basedOn w:val="1"/>
		<w:next w:val="3"/>
		<w:unhideWhenUsed/>
		<w:qFormat/>
		<w:uiPriority w:val="9"/>
		<w:pPr>
			<w:keepNext/>
			<w:keepLines/>
			<w:spacing w:before="300"/>
			<w:outlineLvl w:val="1"/>
		</w:pPr>
		<w:rPr>
			<w:rFonts w:cstheme="majorBidi" />
			<w:b/>
			<w:bCs/>
			<w:sz w:val="32"/>
			<w:szCs w:val="32"/>
		</w:rPr>
	</w:style>
	<w:style w:customStyle="1" w:styleId="78" w:type="paragraph">
		<w:name w:val="heading 2 center"/>
		<w:basedOn w:val="5"/>
		<w:qFormat/>
		<w:uiPriority w:val="0"/>
		<w:pPr>
			<w:jc w:val="center"/>
		</w:pPr>
	</w:style>
	<w:style w:customStyle="1" w:styleId="80" w:type="paragraph">
		<w:name w:val="Body Text center"/>
		<w:basedOn w:val="3"/>
		<w:qFormat/>
		<w:uiPriority w:val="0"/>
		<w:pPr>
			<w:jc w:val="center"/>
		</w:pPr>
	</w:style>

final result in document.xml, hello sample h2 for center alignment styleId should be 78 while the text body text for center alignment should be 80

   <w:bookmarkStart w:id="20" w:name="hello-sample-h2-by-default" />
    <w:p><w:pPr><w:pStyle w:val="5" /></w:pPr><w:r><w:t xml:space="preserve">hello sample h2 by default</w:t></w:r></w:p>
    <w:bookmarkEnd w:id="20" />
    <w:bookmarkStart w:id="21" w:name="hello-sample-h2-for-center-alignment" />
    <w:p><w:pPr><w:pStyle **w:val="5"** /></w:pPr><w:r><w:t xml:space="preserve">hello sample h2 for center alignment</w:t></w:r></w:p>
    <w:p><w:pPr><w:pStyle **w:val="78"** /></w:pPr><w:r><w:t xml:space="preserve">body text for center alignment</w:t></w:r></w:p>

Pandoc version?
system: MacOS
pandoc 3.6.4
Features: +server +lua
Scripting engine: Lua 5.4

@gaiyinaizhi gaiyinaizhi added the bug label Apr 2, 2025
@jgm
Copy link
Owner

jgm commented Apr 2, 2025

Can you upload the template.docx you are using, so I can try to reproduce this?

@gaiyinaizhi
Copy link
Author

template.docx @jgm

@jgm
Copy link
Owner

jgm commented Apr 2, 2025

A preliminary observation:

% pandoc -f html -t native
<h2>hello sample h2 by default</h2>
<h2 custom-style="heading 2 center">hello sample h2 for center alignment</h2>
<p custom-style="Body Text center">body text for center alignment</p>
^D
[ Header
    2
    ( "hello-sample-h2-by-default" , [] , [] )
    [ Str "hello"
    , Space
    , Str "sample"
    , Space
    , Str "h2"
    , Space
    , Str "by"
    , Space
    , Str "default"
    ]
, Header
    2
    ( "hello-sample-h2-for-center-alignment"
    , []
    , [ ( "custom-style" , "heading 2 center" ) ]
    )
    [ Str "hello"
    , Space
    , Str "sample"
    , Space
    , Str "h2"
    , Space
    , Str "for"
    , Space
    , Str "center"
    , Space
    , Str "alignment"
    ]
, Para
    [ Str "body"
    , Space
    , Str "text"
    , Space
    , Str "for"
    , Space
    , Str "center"
    , Space
    , Str "alignment"
    ]
]

Note that the custom-style attribute on the p element is not parsed; this is because Para in pandoc doesn't have a slot for attributes. (You could try using an enclosing div instead.)

But I can confirm that there is some kind of bug here; here's the trace of the function getStyleIdFromName:

ParaStyleName (CIString "Heading 2")
ParaStyleId "5"
ParaStyleName (CIString "Heading 2")
ParaStyleId "5"
ParaStyleName (CIString "heading 2 center")
ParaStyleId "78"

The second block ought to be getting "heading 2 center".

@jgm jgm pinned this issue Apr 7, 2025
@jgm jgm unpinned this issue Apr 7, 2025
@jgm jgm modified the milestone: next release Apr 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants