class Rundoc::Document
Represents a single rundoc file on disk,
Each document contains one or more fenced code blocks. Those are parsed as ‘FencedCodeBlock` instances and then executed.
Constants
- CODEBLOCK_REGEX
- GITHUB_BLOCK
- PARTIAL_RESULT
Attributes
Public Class Methods
Source
# File lib/rundoc/document.rb, line 14 def initialize(contents, context:) @context = context @contents = contents @original = contents.dup @stack = [] partition PARTIAL_RESULT.clear end
Source
# File lib/rundoc/document.rb, line 40 def self.partial_result_to_doc out = to_doc(result: PARTIAL_RESULT) unfinished = FencedCodeBlock.partial_result_to_doc out << unfinished if unfinished out end
Source
# File lib/rundoc/document.rb, line 47 def self.to_doc(result:) result.join("") end
Public Instance Methods
Source
# File lib/rundoc/document.rb, line 52 def partition until contents.empty? head, code, tail = contents.partition(CODEBLOCK_REGEX) @stack << head unless head.empty? unless code.empty? match = code.match(CODEBLOCK_REGEX) @stack << FencedCodeBlock.new( fence: match[:fence], lang: match[:lang], code: match[:contents], context: context ) end @contents = tail end end
split into [before_code, code, after_code], process code, and re-run until tail is empty
Source
# File lib/rundoc/document.rb, line 23 def to_md result = [] @stack.each do |s| result << if s.respond_to?(:render) s.render else s end PARTIAL_RESULT.replace(result) end self.class.to_doc(result: result) rescue => e File.write("README.md", result.join("")) raise e end