gettalong / kramdown

kramdown is a fast, pure Ruby Markdown superset converter, using a strict syntax definition and supporting several common extensions.
http://kramdown.gettalong.org
Other
1.72k stars 274 forks source link

Allow to specify encoding. #662

Closed ioquatix closed 4 years ago

ioquatix commented 4 years ago

https://github.com/gettalong/kramdown/blob/4458c23153884cb576e4a57739e191317256abea/lib/kramdown/converter/base.rb#L110

In order to convert a small part of the document, I had to copy the encoding:

> Kramdown::Converter::Kramdown.convert(root.children.first)
Traceback (most recent call last):
       16: from /home/samuel/.gem/ruby/2.7.1/gems/utopia-2.15.1/lib/utopia/controller/actions.rb:154:in `block in dispatch'
       15: from /home/samuel/.gem/ruby/2.7.1/gems/utopia-2.15.1/lib/utopia/controller/actions.rb:154:in `instance_exec'
       14: from /home/samuel/Documents/socketry/utopia-project/pages/examples/controller.rb:13:in `block in load_controller_file'
       13: from /home/samuel/Documents/socketry/utopia-project/pages/examples/controller.rb:13:in `find'
       12: from /home/samuel/Documents/socketry/utopia-project/pages/examples/controller.rb:13:in `each'
       11: from /home/samuel/Documents/socketry/utopia-project/lib/utopia/project/base.rb:154:in `examples'
       10: from /home/samuel/Documents/socketry/utopia-project/lib/utopia/project/base.rb:154:in `each_child'
        9: from /home/samuel/Documents/socketry/utopia-project/lib/utopia/project/base.rb:159:in `block in examples'
        8: from /home/samuel/Documents/socketry/utopia-project/lib/utopia/project/base.rb:159:in `new'
        7: from /home/samuel/Documents/socketry/utopia-project/lib/utopia/project/example.rb:44:in `initialize'
        6: from /home/samuel/Documents/socketry/utopia-project/lib/utopia/project/example.rb:59:in `document'
        5: from /home/samuel/Documents/socketry/utopia-project/lib/utopia/project/example.rb:59:in `tap'
        4: from /home/samuel/Documents/socketry/utopia-project/lib/utopia/project/example.rb:72:in `block in document'
        3: from <internal:prelude>:20:in `irb'
        2: from /home/samuel/Documents/socketry/utopia-project/lib/utopia/project/example.rb:5:in `block in document'
        1: from /home/samuel/.gem/ruby/2.7.1/gems/kramdown-2.2.1/lib/kramdown/converter/base.rb:110:in `convert'
Kramdown::Error (Missing encoding option on root element)

> root.children.first.options[:encoding] = root.options[:encoding]

> root.options
=> {:encoding=>#<Encoding:UTF-8>, :location=>1, :options=>{}, :abbrev_defs=>{}, :abbrev_attr=>{}, :footnote_count=>0}

> Kramdown::Converter::Kramdown.convert(root.children.first)
=> ["`utopia-project` easily integrates with GitHub Pages.\n\n", []]

Maybe you could allow:

convert(node, encoding: Encoding::UTF_8)
gettalong commented 4 years ago

See https://github.com/gettalong/kramdown/blob/4458c23153884cb576e4a57739e191317256abea/lib/kramdown/element.rb#L24-L45 for information on the root element. It not only has the :encoding option but also others.

So if you only want to convert a small part, duplicate the root node and assign the node you want to convert as children, ie. new_root = root.dup; new_root.children = [node].

ioquatix commented 4 years ago

Should we add documentation on how to do this?

gettalong commented 4 years ago

That is a good idea - will see about it.

gettalong commented 4 years ago

Done