Over a million developers have joined DZone.

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

  def to_md
  alias_method :to_markdown, :to_md

# 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")

  # convert the block to markdown format
  # and also give it a full name alias
  def to_md
  alias_method :to_markdown, :to_md


# 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

  # 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

  # 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

  # 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|
        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]

          add_code line


  # 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

    # 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

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'

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}