Class: Paru::PandocFilter::CodeBlock
- Includes:
- InnerMarkdown
- Defined in:
- lib/paru/filter/code_block.rb
Overview
A CodeBlock is a Block level node with an attribute object and the code as a string
Instance Attribute Summary collapse
Class Method Summary collapse
-
.from_code_string(code_string, language = "") ⇒ CodeBlock
Create a new CodeBlock based on a string and, optionally, a language.
-
.from_file(filename, language = "") ⇒ CodeBlock
Create a new CodeBlock based on the contents of a file, and, optionally, a language.
Instance Method Summary collapse
-
#ast_contents ⇒ Object
An AST representation of this CodeBlock.
-
#has_string? ⇒ Boolean
Has this CodeBlock string contents?.
-
#initialize(contents) ⇒ CodeBlock
constructor
Create a new CodeBlock based on the contents.
-
#inner_markdown ⇒ String
included
from InnerMarkdown
Get the markdown representation of this Node's children.
-
#inner_markdown=(markdown) ⇒ Object
included
from InnerMarkdown
Replace this Node's children with the Nodes represented by the markdown string.
-
#to_code_string ⇒ String
Get this CodeBlock's contents as a string.
-
#to_file(filename) ⇒ Object
Write this CodeBlock's contents to file.
Constructor Details
Instance Attribute Details
#attr ⇒ Attr
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 |
# File 'lib/paru/filter/code_block.rb', line 33 class CodeBlock < Block include InnerMarkdown attr_accessor :attr, :string # Create a new CodeBlock based on the contents # # @param contents [Array] an array with the attribute and the code # string def initialize(contents) @attr = Attr.new contents[0] @string = contents[1] end # An AST representation of this CodeBlock def ast_contents() [ @attr.to_ast, @string ] end # Has this CodeBlock string contents? # # @return [Boolean] true def has_string?() true end # Write this CodeBlock's contents to file # # @param filename {String} the path to the file to write def to_file(filename) File.open(filename, "w") do |file| file.write "#{@string}\n" end end # Create a new CodeBlock based on the contents of a file, and, # optionally, a language # # @param filename {String} the path to the file to read the # contents from # @param language {String} the language of the contents # # @return [CodeBlock] def self.from_file(filename, language = "") return self.from_code_string(File.read(filename), language) end # Get this CodeBlock's contents as a string # # @return [String] def to_code_string() return @string end # Create a new CodeBlock based on a string and, optionally, a # language # # # @param code_string [String] the string with code to use as the # contents of the CodeBlock # @param language [String] the optional language class # @return [CodeBlock] def self.from_code_string(code_string, language = "") attributes = ["", [language], []] code_block = CodeBlock.new [attributes, code_string] return code_block end end |
#string ⇒ String
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 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 |
# File 'lib/paru/filter/code_block.rb', line 33 class CodeBlock < Block include InnerMarkdown attr_accessor :attr, :string # Create a new CodeBlock based on the contents # # @param contents [Array] an array with the attribute and the code # string def initialize(contents) @attr = Attr.new contents[0] @string = contents[1] end # An AST representation of this CodeBlock def ast_contents() [ @attr.to_ast, @string ] end # Has this CodeBlock string contents? # # @return [Boolean] true def has_string?() true end # Write this CodeBlock's contents to file # # @param filename {String} the path to the file to write def to_file(filename) File.open(filename, "w") do |file| file.write "#{@string}\n" end end # Create a new CodeBlock based on the contents of a file, and, # optionally, a language # # @param filename {String} the path to the file to read the # contents from # @param language {String} the language of the contents # # @return [CodeBlock] def self.from_file(filename, language = "") return self.from_code_string(File.read(filename), language) end # Get this CodeBlock's contents as a string # # @return [String] def to_code_string() return @string end # Create a new CodeBlock based on a string and, optionally, a # language # # # @param code_string [String] the string with code to use as the # contents of the CodeBlock # @param language [String] the optional language class # @return [CodeBlock] def self.from_code_string(code_string, language = "") attributes = ["", [language], []] code_block = CodeBlock.new [attributes, code_string] return code_block end end |
Class Method Details
.from_code_string(code_string, language = "") ⇒ CodeBlock
Create a new CodeBlock based on a string and, optionally, a language
97 98 99 100 101 |
# File 'lib/paru/filter/code_block.rb', line 97 def self.from_code_string(code_string, language = "") attributes = ["", [language], []] code_block = CodeBlock.new [attributes, code_string] return code_block end |
.from_file(filename, language = "") ⇒ CodeBlock
Create a new CodeBlock based on the contents of a file, and, optionally, a language
78 79 80 |
# File 'lib/paru/filter/code_block.rb', line 78 def self.from_file(filename, language = "") return self.from_code_string(File.read(filename), language) end |
Instance Method Details
#ast_contents ⇒ Object
An AST representation of this CodeBlock
47 48 49 50 51 52 |
# File 'lib/paru/filter/code_block.rb', line 47 def ast_contents() [ @attr.to_ast, @string ] end |
#has_string? ⇒ Boolean
Has this CodeBlock string contents?
57 58 59 |
# File 'lib/paru/filter/code_block.rb', line 57 def has_string?() true end |
#inner_markdown ⇒ String Originally defined in module InnerMarkdown
Get the markdown representation of this Node's children.
#inner_markdown=(markdown) ⇒ Object Originally defined in module InnerMarkdown
Replace this Node's children with the Nodes represented by the markdown string
#to_code_string ⇒ String
Get this CodeBlock's contents as a string
85 86 87 |
# File 'lib/paru/filter/code_block.rb', line 85 def to_code_string() return @string end |
#to_file(filename) ⇒ Object
Write this CodeBlock's contents to file
64 65 66 67 68 |
# File 'lib/paru/filter/code_block.rb', line 64 def to_file(filename) File.open(filename, "w") do |file| file.write "#{@string}\n" end end |