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



35
36
37
# File 'lib/codemark/rmd_to_r.rb', line 35

def header(text, header_level)
  "#" + "*"*header_level + " " + text + "\n\n"
end

#hruleObject



39
40
41
# File 'lib/codemark/rmd_to_r.rb', line 39

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

#paragraph(text) ⇒ Object

def list_item(text, list_type)

text

end



51
52
53
54
55
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
# File 'lib/codemark/rmd_to_r.rb', line 51

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
# File 'lib/codemark/rmd_to_r.rb', line 7

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