Module: MaRuKu::Out::Markdown
- Included in:
- MDElement
- Defined in:
- lib/amp-front/third_party/maruku.rb,
lib/amp-front/third_party/maruku/output/to_markdown.rb
Overview
Functions for exporting to MarkDown.
Constant Summary collapse
- DefaultLineLength =
40
Instance Method Summary collapse
- #add_indent(s, char = " ") ⇒ Object
- #array_to_md(array, context, join_char = '') ⇒ Object
-
#children_to_md(context) ⇒ Object
Convert each child to html.
- #to_md(context = {}) ⇒ Object
- #to_md_abbr_def(context) ⇒ Object
- #to_md_li_span(context) ⇒ Object
- #to_md_ol(context) ⇒ Object
- #to_md_paragraph(context) ⇒ Object
- #to_md_ul(context) ⇒ Object
- #wrap(array, line_length, context) ⇒ Object
Instance Method Details
#add_indent(s, char = " ") ⇒ Object
85 86 87 88 89 |
# File 'lib/amp-front/third_party/maruku/output/to_markdown.rb', line 85 def add_indent(s,char=" ") t = s.split("\n").map{|x| char+x }.join("\n") s << ?\n if t[-1] == ?\n s end |
#array_to_md(array, context, join_char = '') ⇒ Object
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/amp-front/third_party/maruku/output/to_markdown.rb', line 127 def array_to_md(array, context, join_char='') e = [] array.each do |c| method = c.kind_of?(MDElement) ? "to_md_#{c.node_type}" : "to_md" if not c.respond_to?(method) #raise "Object does not answer to #{method}: #{c.class} #{c.inspect[0,100]}" # tell_user "Using default for #{c.node_type}" method = 'to_md' end # puts "#{c.inspect} created with method #{method}" h = c.send(method, context) if h.nil? raise "Nil md for #{c.inspect} created with method #{method}" end if h.kind_of?Array e = e + h else e << h end end e.join(join_char) end |
#children_to_md(context) ⇒ Object
Convert each child to html
92 93 94 |
# File 'lib/amp-front/third_party/maruku/output/to_markdown.rb', line 92 def children_to_md(context) array_to_md(@children, context) end |
#to_md(context = {}) ⇒ Object
39 40 41 |
# File 'lib/amp-front/third_party/maruku/output/to_markdown.rb', line 39 def to_md(context={}) children_to_md(context) end |
#to_md_abbr_def(context) ⇒ Object
55 56 57 |
# File 'lib/amp-front/third_party/maruku/output/to_markdown.rb', line 55 def to_md_abbr_def(context) "*[#{self.abbr}]: #{self.text}\n" end |
#to_md_li_span(context) ⇒ Object
48 49 50 51 52 53 |
# File 'lib/amp-front/third_party/maruku/output/to_markdown.rb', line 48 def to_md_li_span(context) len = (context[:line_length] || DefaultLineLength) - 2 s = add_tabs(wrap(@children, len-2, context), 1, ' ') s[0] = ?* s + "\n" end |
#to_md_ol(context) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/amp-front/third_party/maruku/output/to_markdown.rb', line 59 def to_md_ol(context) len = (context[:line_length] || DefaultLineLength) - 2 md = "" self.children.each_with_index do |li, i| s = add_tabs(w=wrap(li.children, len-2, context), 1, ' ')+"\n" s[0,4] = "#{i+1}. "[0,4] # puts w.inspect md += s end md + "\n" end |
#to_md_paragraph(context) ⇒ Object
43 44 45 46 |
# File 'lib/amp-front/third_party/maruku/output/to_markdown.rb', line 43 def to_md_paragraph(context) line_length = context[:line_length] || DefaultLineLength wrap(@children, line_length, context)+"\n" end |
#to_md_ul(context) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/amp-front/third_party/maruku/output/to_markdown.rb', line 71 def to_md_ul(context) len = (context[:line_length] || DefaultLineLength) - 2 md = "" self.children.each_with_index do |li, i| w = wrap(li.children, len-2, context) # puts "W: "+ w.inspect s = add_indent(w) # puts "S: " +s.inspect s[0,1] = "-" md += s end md + "\n" end |
#wrap(array, line_length, context) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/amp-front/third_party/maruku/output/to_markdown.rb', line 96 def wrap(array, line_length, context) out = "" line = "" array.each do |c| if c.kind_of?(MDElement) && c.node_type == :linebreak out << line.strip << " \n"; line=""; next end pieces = if c.kind_of? String c.to_md.mysplit else [c.to_md(context)].flatten end # puts "Pieces: #{pieces.inspect}" pieces.each do |p| if p.size + line.size > line_length out << line.strip << "\n"; line = "" end line << p end end out << line.strip << "\n" if line.size > 0 out << ?\n if not out[-1] == ?\n out end |