What's the best way of handling body formats for long text documents? #564
|
Hi! I'm planning on using Phlex to generate HTML for a pdf document. I want to be able to write a body on a format that keeps newlines and where it's possible to e.g. make single words bold without dividing up the text into different parts. I might have missed something but my current solution involves This is an example document HTML generator, which will be used as class PhlexDocument < Phlex::HTML
def template
render Document.new do |doc|
doc.body do
<<~TEXT
It should keep
newlines
It can be <b>bold</b>
The end.
TEXT
end
end
end
endAnd this is my Document class: class Document < Phlex::HTML
include Phlex::DeferredRender
include ActionView::Helpers::TextHelper
def template
if @body
unsafe_raw(
simple_format(
@body.call
)
)
end
end
def body(&block)
@body = block
end
endIs there a better way to do this? Thanks in advance! |
Replies: 1 comment 1 reply
|
This looks reasonable to me. I wonder if You may want to consider making the render Document.new(<<~TEXT)
Document text here.
TEXTYou may also want to look into the phlex-markdown gem, which I created while building the phlex documentation site. This is very much still in beta and subject to change, but it’s also incredibly simple, so there’s not too much that can go wrong. You could even just copy the implementation, which is entirely in this one file. |
This looks reasonable to me. I wonder if
simple_formathas been misclassified as a “value helper” rather than an “output helper” in theSimpleFormatadapter. Ifsimple_formatis considered HTML safe (I assume it only allows safe HTML but will need to confirm this), we could make theunsafe_rawcall in the adapter itself.You may want to consider making the
bodyan argument rather than a method. The interface could then be:You may also want to look into the phlex-markdown gem, which I created while building the phlex documentation site. This is very much still in beta and subject to change, but it’s also incredibly simple, so there’s…