Class: Rundoc::CodeSection
- Inherits:
-
Object
- Object
- Rundoc::CodeSection
- Defined in:
- lib/rundoc/code_section.rb
Overview
A code secttion respesents a block of fenced code
A document can have multiple code sections
Defined Under Namespace
Classes: ParseError
Constant Summary collapse
- COMMAND_REGEX =
todo: move whole thing
Rundoc::Parser::COMMAND_REGEX
- AUTOGEN_WARNING =
"\n<!-- STOP. This document is autogenerated. Do not manually modify. See the top of the doc for more details. -->"
- PARTIAL_RESULT =
[]
- PARTIAL_ENV =
{}
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#commands ⇒ Object
Returns the value of attribute commands.
-
#fence ⇒ Object
Returns the value of attribute fence.
-
#keyword ⇒ Object
Returns the value of attribute keyword.
-
#lang ⇒ Object
Returns the value of attribute lang.
-
#original ⇒ Object
Returns the value of attribute original.
Class Method Summary collapse
Instance Method Summary collapse
-
#hidden? ⇒ Boolean
all of the commands are hidden.
-
#initialize(match, keyword:, context:) ⇒ CodeSection
constructor
A new instance of CodeSection.
-
#not_hidden? ⇒ Boolean
one or more of the commands are not hidden.
- #parse_code_command ⇒ Object
- #render ⇒ Object
Constructor Details
#initialize(match, keyword:, context:) ⇒ CodeSection
Returns a new instance of CodeSection.
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/rundoc/code_section.rb', line 38 def initialize(match, keyword:, context:) @original = match.to_s @commands = [] @stack = [] @keyword = keyword @context = context @fence = match[:fence] @lang = match[:lang] @code = match[:contents] parse_code_command PARTIAL_RESULT.clear PARTIAL_ENV.clear end |
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
33 34 35 |
# File 'lib/rundoc/code_section.rb', line 33 def code @code end |
#commands ⇒ Object
Returns the value of attribute commands.
33 34 35 |
# File 'lib/rundoc/code_section.rb', line 33 def commands @commands end |
#fence ⇒ Object
Returns the value of attribute fence.
33 34 35 |
# File 'lib/rundoc/code_section.rb', line 33 def fence @fence end |
#keyword ⇒ Object
Returns the value of attribute keyword.
33 34 35 |
# File 'lib/rundoc/code_section.rb', line 33 def keyword @keyword end |
#lang ⇒ Object
Returns the value of attribute lang.
33 34 35 |
# File 'lib/rundoc/code_section.rb', line 33 def lang @lang end |
#original ⇒ Object
Returns the value of attribute original.
33 34 35 |
# File 'lib/rundoc/code_section.rb', line 33 def original @original end |
Class Method Details
.partial_result_to_doc ⇒ Object
89 90 91 |
# File 'lib/rundoc/code_section.rb', line 89 def self.partial_result_to_doc to_doc(result: PARTIAL_RESULT, env: PARTIAL_ENV) end |
.to_doc(result:, env:) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/rundoc/code_section.rb', line 93 def self.to_doc(result:, env:) array = [env[:before]] result.flatten! result.compact! result.map! { |s| s.respond_to?(:rstrip) ? s.rstrip : s } result.reject!(&:empty?) result.map!(&:to_s) if !result.empty? array << env[:fence_start] array << result array << env[:fence_end] end array << env[:after] array.flatten! array.compact! array.map! { |s| s.respond_to?(:rstrip) ? s.rstrip : s } array.reject!(&:empty?) array.map!(&:to_s) array.join("\n") << "\n" end |
Instance Method Details
#hidden? ⇒ Boolean
all of the commands are hidden
119 120 121 |
# File 'lib/rundoc/code_section.rb', line 119 def hidden? !not_hidden? end |
#not_hidden? ⇒ Boolean
one or more of the commands are not hidden
124 125 126 127 |
# File 'lib/rundoc/code_section.rb', line 124 def not_hidden? return true if commands.empty? commands.map(&:not_hidden?).detect { |c| c } end |
#parse_code_command ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/rundoc/code_section.rb', line 129 def parse_code_command parser = Rundoc::PegParser.new.code_block tree = parser.parse(@code) actual = Rundoc::PegTransformer.new.apply(tree) actual = [actual] unless actual.is_a?(Array) actual.each do |code_command| @stack << "\n" if commands.last.is_a?(Rundoc::CodeCommand) @stack << code_command commands << code_command end rescue ::Parslet::ParseFailed => e raise "Could not compile code:\n#{@code}\nReason: #{e.}" end |
#render ⇒ Object
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 |
# File 'lib/rundoc/code_section.rb', line 52 def render result = [] env = {} env[:commands] = [] env[:fence_start] = "#{fence}#{lang}" env[:fence_end] = "#{fence}#{AUTOGEN_WARNING}" env[:before] = [] env[:after] = [] env[:context] = @context @stack.each do |s| unless s.respond_to?(:call) result << s next end code_command = s code_output = code_command.call(env) || "" code_line = code_command.to_md(env) || "" env[:commands] << {object: code_command, output: code_output, command: code_line} tmp_result = [] tmp_result << code_line if code_command.render_command? tmp_result << code_output if code_command.render_result? result << tmp_result unless code_command.hidden? PARTIAL_RESULT.replace(result) PARTIAL_ENV.replace(env) end return "" if hidden? self.class.to_doc(result: result, env: env) end |