Re: Ruby Quiz - Challenge #16 - Build the Manuscripts Book Manifest for Documents in Markdown

$ ruby test.rb
Run options: --seed 11990
Finished in 0.004181s, 239.1899 runs/s, 239.1899 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

Reprising the tree-based solution from Challenge #6:

  require 'tree'

  def build(txt)
    _d(_f(_h(txt))).to_yaml #tap{|y| puts y}
  end

  def _d(r)
    r.children.map do |c|
      (g = _d(c)).size > 0 \
      ? {'title' => c.name[1], 'sections' => g} \
      : {'title' => c.name[1]}
    end
  end

  def _f(h, n = Tree::TreeNode.new([0, nil]))
    return n.root unless h[0]

    c = Tree::TreeNode.new(h[0])

    case h[0][0] <=> n.name[0]
    when 1
      n << c
    when 0
      n.parent << c
    else
      n.parent.parent << c
    end

    _f(h[1..-1], c)
  end

  def _h(txt)
    txt
    .lines
    .reject{|line| line =~ /^```/ ... line =~ /^```/ ? true : nil}
    .grep(/^#/)
    .map{|l| l.match(/^(#+)\s+(.*)\s*$/); [$1.length, $2.delete(?")]}
  end

···

On 2020-01-24 13:49, Gerald Bauer wrote:

Challenge #16 - https://github.com/planetruby/quiz/tree/master/016

Hello,

  Welcome back. Prosit 2020!

Reprising the tree-based solution from Challenge #6:

Great to see that you remember the Ruby Quiz Challenge #6 from last
year that only included the first phase (parsing the structured text)
without the second phase (code generation).

  Hard to compete with your solution. Looks great. Building up an
"abstract syntax tree" and then generating the output.
  Thanks again for sharing. Keep it up. Cheers.