Sha256: b5b7d9134516a1b3fe7aeb8da2fd91f4802ec0188c0f87369225e5b122a9a61a

Contents?: true

Size: 1.13 KB

Versions: 41

Compression:

Stored size: 1.13 KB

Contents

# Parses the grammar into a fancy markdown document.

class Markdownify
  
  def initialize file
    @file = file
    @line_num = 0
    @markdowning = false
    @codeblock = false
  end
  
  def format!
    line_num=0
    markdowning = false
    File.open(@file).each do |line|
      if line =~ /^\#STOP_MARKDOWN/
        @markdowning = false
      end
      if markdowning? && !(line =~ /^\s+$/)
        print format_line(line)
      end
      if line =~ /^\#START_MARKDOWN/
        @markdowning = true
      end
    end
    finish_code_block if @codeblock
  end
  
  def markdowning?
    @markdowning
  end
  
  def format_line(line)
    if line =~ /\s*\#/
      finish_code_block if @codeblock
      @codeblock = false
      format_doc line
    else
      start_code_block unless @codeblock
      @codeblock = true
      format_bnf line
    end
  end
  
  def format_doc line
    line.sub(/\s*\#\s*/, '')
  end
  
  def format_bnf line
    bnf = line.gsub(/\{.+\}/, '')
    "   #{bnf}"
  end

  def start_code_block
    print "\n\n```\n"
  end
  
  def finish_code_block 
    print "```\n\n"
  end
end

Markdownify.new('lib/sparkql/parser.y').format!

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
sparkql-1.3.0 script/markdownify.rb
sparkql-1.2.8 script/markdownify.rb
sparkql-1.2.7 script/markdownify.rb
sparkql-1.2.6 script/markdownify.rb
sparkql-1.2.5 script/markdownify.rb
sparkql-1.2.4 script/markdownify.rb
sparkql-1.2.3 script/markdownify.rb
sparkql-1.2.2 script/markdownify.rb
sparkql-1.2.1 script/markdownify.rb
sparkql-1.2.0 script/markdownify.rb
sparkql-1.1.17 script/markdownify.rb
sparkql-1.1.16 script/markdownify.rb
sparkql-1.1.15 script/markdownify.rb
sparkql-1.1.14 script/markdownify.rb
sparkql-1.1.13 script/markdownify.rb
sparkql-1.1.12 script/markdownify.rb
sparkql-1.1.11 script/markdownify.rb
sparkql-1.1.10 script/markdownify.rb
sparkql-1.1.9 script/markdownify.rb
sparkql-1.1.8 script/markdownify.rb