Class: CodeMark::MarkdownToR

Inherits:
Redcarpet::Render::Base
  • Object
show all
Defined in:
lib/codemark/rmd_to_r.rb

Instance Method Summary collapse

Instance Method Details

#header(text, header_level) ⇒ Object

def footnote_def(content, number)

content

end



36
37
38
39
40
41
42
# File 'lib/codemark/rmd_to_r.rb', line 36

def header(text, header_level)
  if (@code_started and !@code_ignore)
    "# " + text + "\n"
  else
    "#" + "*"*header_level + " " + text + "\n\n"
  end
end

#hruleObject



44
45
46
# File 'lib/codemark/rmd_to_r.rb', line 44

def hrule()
  '#' + '-'*50
end

#paragraph(text) ⇒ Object

def list_item(text, list_type)

text

end



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/codemark/rmd_to_r.rb', line 56

def paragraph(text)
  return nil if text == '\newpage'


  # codeblock started at this para?
  start_code = text.match(/^```{r(?<code_spec>[^}]*)}[\n]*(?<code>.*)/m)
  if start_code
    @code_started = true
    @code_ignore = false
    if start_code[:code_spec].match(/echo[ ]*=[ ]*(f|F)/)
      @code_ignore = true
    end

    text = start_code[:code]
  end

  # codeblock ends with this para?
  end_code = text.match(/(?<code>.*)\n```$/m)
  if end_code
    text = end_code[:code]
    @code_ends = true
  end

  # Comment out regular text
  unless @code_started
    text = "# " + text

    # Wrap regular text with commented lines
    if text.length > 80
      final = text.split(' ').reduce({full:'', sentence:''}) do |sofar, word|
        possible_sentence = sofar[:sentence] + " " + word
        if (possible_sentence).length <= 80
          sofar[:sentence] = possible_sentence
        else
          sofar[:full] = sofar[:full] + "\n" + sofar[:sentence]
          sofar[:sentence] = "# " + word
        end
        sofar
      end

      text = final[:full].strip + "\n" + final[:sentence]
    end

  end

  if @code_ends
    @code_started = @code_ends = false
  end

  unless @code_ignore
    text + "\n\n"
  end
end

#preprocess(full_document) ⇒ Object



7
8
9
10
11
12
13
14
# File 'lib/codemark/rmd_to_r.rb', line 7

def preprocess(full_document)
  # strip headers
  # headers_rx = "^---\n([\sa-zA-Z0-9_]+:.[^\n]+\n)+---\n"
  headers_rx = "^---\n.*---\n"
  regx = /(?<headers>#{headers_rx})(?<body>.*)/m
  doc = full_document.match(regx)
  doc[:body]
end