Skip to content

Instantly share code, notes, and snippets.

@dchacke
Last active May 4, 2021 05:15
Show Gist options
  • Save dchacke/277786be964d1ca3a2c8e9412bf4b618 to your computer and use it in GitHub Desktop.
Save dchacke/277786be964d1ca3a2c8e9412bf4b618 to your computer and use it in GitHub Desktop.

Revisions

  1. Dennis Hackethal revised this gist May 4, 2021. 1 changed file with 10 additions and 10 deletions.
    20 changes: 10 additions & 10 deletions hiccup_to_html.rb
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,18 @@
    def to_html structure
    if structure.is_a? Hash
    structure.reduce([]) do |acc, curr|
    acc + [[curr[0].to_s, '="', curr[1].to_s, '"'].join]
    structure.reduce([]) do |acc, (key, val)|
    acc + [[key.to_s, '="', val.to_s, '"'].join]
    end.join(' ')
    elsif structure.is_a? Array
    tag = to_html structure[0]
    attrs = to_html structure[1]
    children = structure.drop(2).map { |s| to_html s }.join
    if structure[1].is_a? Hash
    tag, attrs, *children = structure.map { |s| to_html s}
    tag_and_attrs = structure[1].any? ? [tag, ' ', attrs].join : tag
    else
    tag, *children = structure.map { |s| to_html s}
    end

    "<#{tag} #{attrs}>#{children}</#{tag}>"
    ['<', tag_and_attrs || tag, '>', children.join, '</', tag, '>'].join
    else
    structure.to_s
    end
    end

    # to_html [:html, {lang: 'en'}, [:h1, {class: 'heading big'}, 'some header']]
    # => "<html lang=\"en\"><h1 class=\"heading big\">some header</h1></html>"
    end
  2. Dennis Hackethal revised this gist May 4, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion hiccup_to_html.rb
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@ def to_html structure
    elsif structure.is_a? Array
    tag = to_html structure[0]
    attrs = to_html structure[1]
    children = structure.drop(2).map { |s| to_html s }.join()
    children = structure.drop(2).map { |s| to_html s }.join

    "<#{tag} #{attrs}>#{children}</#{tag}>"
    else
  3. Dennis Hackethal created this gist May 4, 2021.
    18 changes: 18 additions & 0 deletions hiccup_to_html.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    def to_html structure
    if structure.is_a? Hash
    structure.reduce([]) do |acc, curr|
    acc + [[curr[0].to_s, '="', curr[1].to_s, '"'].join]
    end.join(' ')
    elsif structure.is_a? Array
    tag = to_html structure[0]
    attrs = to_html structure[1]
    children = structure.drop(2).map { |s| to_html s }.join()

    "<#{tag} #{attrs}>#{children}</#{tag}>"
    else
    structure.to_s
    end
    end

    # to_html [:html, {lang: 'en'}, [:h1, {class: 'heading big'}, 'some header']]
    # => "<html lang=\"en\"><h1 class=\"heading big\">some header</h1></html>"