class Rdoc2md::Document
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 (kirkbowers@yahoo.com)
- Copyright
-
Copyright © 2014 Frabjous Apps LLC
- License
-
MIT License
Attributes
text[RW]
The text is the document to be converted from rdoc style to markdown. It should be a String.
Public Class Methods
new(text = "")
click to toggle source
The initializer takes an optional text, which is the document to be converted from rdoc style to markdown.
# File lib/rdoc2md.rb, line 18 def initialize(text = "") @text = text end
Public Instance Methods
to_md()
click to toggle source
Convert the existing document to markdown. The result is returned as a String.
# 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