Module: LiterateRuby

Defined in:
lib/literate-ruby.rb,
lib/literate-ruby/version.rb

Constant Summary collapse

INDENTED_CODE_REGEX =
/ # Match a MARKDOWN CODE section.
(\r?\n)              # $1: CODE must be preceded by blank line
(                    # $2: CODE contents
  (?:                # Group for multiple lines of code.
    (?:\r?\n)+       # Each line preceded by a newline,
    (?:[ ]{4}|\t).*  # and begins with four spaces or tab.
  )+                 # One or more CODE lines
  \r?\n              # CODE folowed by blank line.
)                    # End $2: CODE contents
(?=\r?\n)?           # CODE folowed by blank line.
/x
VERSION =
"0.0.1"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.basenameObject (readonly)

Returns the value of attribute basename.



6
7
8
# File 'lib/literate-ruby.rb', line 6

def basename
  @basename
end

.htmlObject (readonly)

Returns the value of attribute html.



6
7
8
# File 'lib/literate-ruby.rb', line 6

def html
  @html
end

.inputObject (readonly)

Returns the value of attribute input.



6
7
8
# File 'lib/literate-ruby.rb', line 6

def input
  @input
end

.rubyObject (readonly)

Returns the value of attribute ruby.



6
7
8
# File 'lib/literate-ruby.rb', line 6

def ruby
  @ruby
end

Class Method Details

.check_argsObject



26
27
28
# File 'lib/literate-ruby.rb', line 26

def check_args
  print_help if ARGV.length == 0
end

.convert_markdownObject



61
62
63
64
65
# File 'lib/literate-ruby.rb', line 61

def convert_markdown
  syntax_highlight_ruby_blocks
  markdown = Kramdown::Document.new @input
  @html = markdown.to_html
end

.export_htmlObject



67
68
69
70
# File 'lib/literate-ruby.rb', line 67

def export_html
  html_filename = File.expand_path("./#{@basename}.html")
  File.write html_filename, @html
end

.export_rubyObject



72
73
74
75
# File 'lib/literate-ruby.rb', line 72

def export_ruby
  ruby_filename = File.expand_path("./#{@basename}.rb")
  File.write ruby_filename, @ruby
end

.extract_rubyObject



41
42
43
44
45
# File 'lib/literate-ruby.rb', line 41

def extract_ruby
  @ruby = @input.scan(INDENTED_CODE_REGEX).join('')
  @ruby.gsub!(/^[ ]{4}/ , '')
  @ruby.lstrip!.strip!
end

.get_inputObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/literate-ruby.rb', line 30

def get_input
  file = ARGV[0]

  if File.exists?(file)
    @input = File.read file
    @basename = File.basename file, '.litrb'
  else
    print_help
  end
end

.highlight_code(code) ⇒ Object



57
58
59
# File 'lib/literate-ruby.rb', line 57

def highlight_code(code)
  Pygments.highlight code, :lexer => 'ruby'
end


20
21
22
23
24
# File 'lib/literate-ruby.rb', line 20

def print_help
  warn "  Usage:"
  warn "    literate-ruby [FILE].litrb"
  exit 1
end

.start!Object



77
78
79
80
81
82
83
84
# File 'lib/literate-ruby.rb', line 77

def start!
  check_args
  get_input
  extract_ruby
  convert_markdown
  export_html
  export_ruby
end

.syntax_highlight_ruby_blocksObject



47
48
49
50
51
52
53
54
55
# File 'lib/literate-ruby.rb', line 47

def syntax_highlight_ruby_blocks
  @input.gsub! INDENTED_CODE_REGEX do |code|
    code.gsub!(/^[ ]{4}/ , '')
    code.strip!.lstrip!

    highlighted_code = highlight_code code
    "\n\n#{highlighted_code}\n"
  end
end