Class: Rdoc2md::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/rdoc2md.rb

Overview

Rdoc2md::Document takes a String representing a document in Rdoc format (sans leading hashmark comments) and converts it into a similar markdown document.

Author

Kirk Bowers ([email protected])

Copyright

Copyright © 2014 Frabjous Apps LLC

License

MIT License

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text = "") ⇒ Document

The initializer takes an optional text, which is the document to be converted from rdoc style to markdown.



18
19
20
# File 'lib/rdoc2md.rb', line 18

def initialize(text = "")
  @text = text
end

Instance Attribute Details

#textObject

The text is the document to be converted from rdoc style to markdown. It should be a String.



14
15
16
# File 'lib/rdoc2md.rb', line 14

def text
  @text
end

Instance Method Details

#to_mdObject

Convert the existing document to markdown. The result is returned as a String.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rdoc2md.rb', line 23

def to_md
  # Usually ruby is extremely readable, but I think "-1" means "give me all the 
  # trailing blank lines" is surprisingly opaque.  That's what the -1 does...
  lines = @text.split("\n", -1)
  lines.collect do |line|
    result = line
 
    # Leave lines that start with 4 spaces alone.  These are code blocks and
    # should pass through unchanged.
    unless result =~ /^\s{4,}/
 
      # Convert headers
      result.sub!(/^(=){1,6}/) { |s| "#" * s.length} unless result =~ /^={7,}/
 
      # Convert strong to have two stars
      # The matching pair of stars should start with a single star that is either at
      # the beginning of the line or not following a backslash, have at least one
      # non-star and non-backslash in between, then end in one star
      result.gsub!(/(\A|[^\\\*])\*([^\*]*[^\*\\])\*/, '\1**\2**')

      # Convert inline code spans to use backticks
      result.gsub!(/(\A|[^\\])\+([^\+]+)\+/, '\1`\2`')

      # Convert bare http:, mailto: and ftp: links
      result.gsub!(/(\A|\s)(http:|https:|mailto:|ftp:)(\S*)/, '\1[\2\3](\2\3)')

      # Convert bare www to an http: link
      result.gsub!(/(\A|\s)www\.(\S*)/, '\1[www.\2](http://www.\2)')

      # Convert link: links to refer to local files
      result.gsub!(/(\A|\s)link:(\S*)/, '\1[\2](\2)')

      # Convert multi word labels surrounded by {} with a url
      result.gsub!(/\{([^\}]*)\}\[(\S*)\]/, '[\1](\2)')

      # Convert one word labels with a url
      result.gsub!(/(\A|\s)([^\{\s]\S*)\[(\S*)\]/, '\1[\2](\3)')

    end
    
    result
  end.join("\n")
end