BaseCampOps / docx_templater

Uses a .docx with special keywords between double pipes || and replaces those pipes and keywords with content.
29 stars 11 forks source link

Replacing Text With Linebreaks #2

Closed mmmries closed 11 years ago

mmmries commented 11 years ago

Originally this was received as an email, I'm converting it to an issue so that we can track the conversation with the project

Hello!

I've been attempting to use your docx_templater, and everything works elegantly! Unfortunately I can't seem to find a way to have a placeholder display a new line.

I've tried the usual \n, \r, \n\r, \r\r and even html tabs like
and &nbsp, but the most they seem to do is display a space, not a new line. Is there any way that I fail to see where we can send one placeholder text including a line break (i.e. to have two paragraphs separated by a space)?

I realize that in general you can just have a second placeholder for the rest and put an empty in between them in the template. However our current project involves the need to insert phrases from a datastore each on their own line, where available phrases could be added by users through our code. This means that unless we dynamically create placeholders (Which I don't think we can) we need to have multiple phrases inserted into the same placeholder, separated by new line characters (usually \n of course).

Thank you for your time!

mmmries commented 11 years ago

I will probably have time to look at this in more detail over the weekend, but if you need the change before then I would suggest you try making a docx file that looks the way you want by hand and then unzip that file so you can look at the raw xml files.

I'm not sure how docx handles linebreaks (it sounds like you tried all the normal text forms of line breaks). So creating it by hand and unzipping should let you look at the xml and see what kind of xml we would need to generate in order to make a newline.

We never needed this feature, but I'm wondering if we want to accept an optional flag that would auto-translate text newlines from a placeholder into the docx equivalent. Maybe something like:

        templater = DocxTemplater.new(convert_newlines: true)
        buffer = templater.replace_file_with_content('path/to/mydocument.docx',
        {
          :client_email1 => 'test@example.com',
          :client_phone1 => '555-555-5555',
          :client_address => "123 Woohoo Ave\nChicago, IL",
        })

@paulcsmith any thoughts?

leducmarc commented 11 years ago

@hqmq Well it looks like in WordprocessingML a linebreak (br, \n and such) is as simple as

 <w:br w:type="text-wrapping"/>

Naturally you need to at the minimum deal with the tags for the text you're in, closing->breaking->reopening so

<w:r><w:t>thisiswordswithoutformatingorbreaks</w:t></w:r>

becomes

<w:r><w:t>thisiswordswithout</w:t> <w:br w:type="text-wrapping"/><w:t>formatingorbreaks</w:t></w:r>

And with my (albeit limited) testing, the above works just fine!

Note that this, a simple line break, is the equivalent to a soft break (shift+enter) in word, NOT the start of a new paragraph. A new paragraph would involve closing off all those sections until the paragraph is closed, then reopening of course. However as we are merely talking about \n this should not only provide a more accurate representation (simply a new line, not a new paragraph) but also be much simpler to implement.

I hope this helps/makes sense and can be implemented ASAP, your work is appreciated! =]

mmmries commented 11 years ago

This is now being addressed in #4, should be released soon

mmmries commented 11 years ago

This is fixed as of the 0.2.0 release. Thanks @giantchiprel for suggesting this improvement.