[kramdown-users] Proposal: block level image tag

Thomas Leitner t_leitner at gmx.at
Sat Jul 17 02:59:23 EDT 2010

On 2010-07-15 16:31 -0400 Eric Sunshine wrote:
> On 07/15/2010 10:24 AM, Thomas Leitner wrote:
> > You are right,<img/>  tags are only allowed inside elements with the
> > Flow/Inline content model and<body>  has the Block content model.
> > Since this was basically a request on the side of LaTeX conversion,
> > what about just leaving nearly everything as-is and just
> > recognizing "block level" image tags only in the LaTeX converter,
> > i.e.: ![caption](foo.png)
> >      [![caption](foo.png)](http://www.example.com)
> > gets converted to HTML like this (no difference to current output):
> >      <p><img alt="caption" src="foo.png" /></p>
> >      <p><a href="http://www.example.com"><img alt="caption"
> > src="foo.png" /></a></p>
> This probably is the correct approach, and certainly is simplest.
> The one thing I liked about the idea of the automatically-generated 
> <div> wrapper was that raw <div>s typically are unstyled (no 
> margin/padding) wheres raw <p>s typically have margin/padding set
> which might "interfere" with desired styling of the enclosed <img/>.
> The <div> wrapper would make it very convenient to interleave blocks
> of text with the occasional image, which is quite common on websites
> and in printed papers. It is, of course, still possible to interleave
> images with text blocks without interference from the default
> paragraph styling either by overriding the styling:
>      ![caption](foo.png)
>      {:.para-with-lone-image}
> or via manual <div> wrapper:
>      <div markdown="1">
>      ![caption](foo.png)
>      </div>
> however, both approaches are rather verbose and ugly for the common
> case of interleaving text bocks and images. On the other hand, the
> added implementation complexity of auto-wrapping the stand-alone
> image in a <div> may be unwarranted, especially as additional
> special-cases (such as the clickable stand-alone image) come to light.

Thanks for your detailed answer. However, I think I will go with the
approach outlined in my previous mail. It seems to be the more correct
approach and if one really needs to have a <div> around a lone image
instead of a paragraph, one can always post-process the kramdown
element tree:

    def para_with_lone_img_to_div(el)
      if el.type == :p && el.children.size == 1 &&
          el.children.first.type == :img
        el.type = :html_element
        el.value = 'div'
        el.children.each {|c| para_with_lone_img_to_div(c)}

    doc = Kramdown::Document.new(text)
    puts doc.to_html

This approach does not work for the case in question with the LaTeX
converter because the output fragment is simply not there.

-- Thomas

More information about the kramdown-users mailing list