Platinum Partner

Convert Ruby scripts to HTML and Markdown

 

#!/usr/bin/env ruby
# Author : Emad Elsaid (https://github.com/blazeeboy)
require 'pygmentize' # gem install pygmentize
require 'redcarpet' # gem install redcarpet

# consecutive lines of code
# should be highlighted using pygmentize
# and converter to markdown block simply
# using the markdown syntax for code
# used by github flavored markdown
class CodeBlock < Array

  def to_html
    Pygmentize.process join, :ruby
  end

  def to_md
    "```ruby\n#{join}\n```"
  end
  alias_method :to_markdown, :to_md
end

# set of consecutive lines of comments
class CommentBlock < Array

  # will use this Redcarpet renderer to convert 
  # markdown to html
  @@renderer = Redcarpet::Markdown.new(Redcarpet::Render::HTML, 
                            fenced_code_blocks: true,
                            disable_indented_code_blocks: true,
                            strikethrough: true,
                            superscript: true,
                            underline: true,
                            autolink: true )

  # use the renderer on the content
  def to_html
    @@renderer.render join("\n")
  end

  # convert the block to markdown format
  # and also give it a full name alias
  def to_md
    join("\n")
  end
  alias_method :to_markdown, :to_md

end 

# container class will have multiple blocks of 
# comments and code
class CodeConverter < Array

  # you should pass code when
  # initializing new instance
  # in order to parse it
  def initialize( code )
    parse code
  end

  # add new line of code to the class
  # it start new code block if the last block is
  # not code block then adds the line of code
  # to the last block
  def add_code( line )
    self << CodeBlock.new unless last.is_a? CodeBlock
    last << line
  end

  # add new comment line to the class
  # it adds new comment block if the last block is not 
  # comments block then add the comment line to the block
  def add_comment( line )
    self << CommentBlock.new unless last.is_a? CommentBlock
    last << line
  end

  # parse code and put result to this class
  # first insert a comment block then
  # parse each line and add it according 
  # to the line type
  def parse(code)
    replace [CommentBlock.new]

    code.lines.each do |line|
      case
        when line.strip.start_with?('# ')
          add_comment line.strip[2..-1]

        when line.strip.start_with?('#!')
          add_code line

        when line.strip.start_with?('#')
          add_comment line.strip[1..-1]

        else
          add_code line
      end
    end

  end

  # convert each block to html
  # and wrap it in a styled page
  # if a file path passed it'll write output to it
  # in all cases it'll return the output
  def to_html(file = nil)
    code = map.with_index do |block, i| 
          # each cell in resulted table 
          # will have a class same as the 
          # class name of teh block downcased
          klass = block.class.to_s.downcase
          # start table row or end it
          # according to the current block index
          prefix = i.even? ? '' : ''
          suffix = i.odd? ? '' : ''
          # produced block html
          "#{prefix}#{block.to_html}#{suffix}" 
        end

    # add a closing cell if the code 
    # ends with comments lines
    code << "" if last.is_a? CommentBlock

    # here is the full page with embeded style
    # i had to add the css here to make sure script stay as 
    # one file not multiple dependent files.
    html = <<-EOT





#{code.join}
EOT # write output to file if a path is passed # and return html File.write file, html unless file.nil? html end # Convert the analyzed code into Markdown # this is easier than HTML as you'll need # to convert each block to markdown and # join them with new line # write output to file if a path is passed def to_md(file=nil) md = map {|b| b.to_md }.join "\n" File.write file, md unless file.nil? md end alias_method :to_markdown, :to_md end # ## How to use ? # create new Code Converter class with code blocks = CodeConverter.new( File.read('/path/to/code/file.rb') ) # and convert to html and markdown and save it to this file path blocks.to_html '/path/to/html/code.html' blocks.to_md '/path/to/md/markdown.md'
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}