class CodeMark::MarkdownToR

Public Instance Methods

header(text, header_level) click to toggle source

def footnote_def(content, number)

content

end

# 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
hrule() click to toggle source
# File lib/codemark/rmd_to_r.rb, line 44
def hrule()
  '#' + '-'*50
end
paragraph(text) click to toggle source

def list_item(text, list_type)

text

end

# 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) click to toggle source
# 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