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
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 35 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.write(filename, "#{@string}\n") 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 = '') from_code_string(File.read(filename), language) end # Get this CodeBlock's contents as a string # # @return [String] def to_code_string @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], []] CodeBlock.new [attributes, code_string] end end |
#string ⇒ String
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 35 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.write(filename, "#{@string}\n") 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 = '') from_code_string(File.read(filename), language) end # Get this CodeBlock's contents as a string # # @return [String] def to_code_string @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], []] CodeBlock.new [attributes, code_string] end end |
Class Method Details
.from_code_string(code_string, language = '') ⇒ CodeBlock
Create a new CodeBlock based on a string and, optionally, a language
98 99 100 101 |
# File 'lib/paru/filter/code_block.rb', line 98 def self.from_code_string(code_string, language = '') attributes = ['', [language], []] CodeBlock.new [attributes, code_string] end |
.from_file(filename, language = '') ⇒ CodeBlock
Create a new CodeBlock based on the contents of a file, and, optionally, a language
79 80 81 |
# File 'lib/paru/filter/code_block.rb', line 79 def self.from_file(filename, language = '') from_code_string(File.read(filename), language) end |
Instance Method Details
#ast_contents ⇒ Object
An AST representation of this CodeBlock
50 51 52 53 54 55 |
# File 'lib/paru/filter/code_block.rb', line 50 def ast_contents [ @attr.to_ast, @string ] end |
#has_string? ⇒ Boolean
Has this CodeBlock string contents?
60 61 62 |
# File 'lib/paru/filter/code_block.rb', line 60 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
86 87 88 |
# File 'lib/paru/filter/code_block.rb', line 86 def to_code_string @string end |
#to_file(filename) ⇒ Object
Write this CodeBlock’s contents to file
67 68 69 |
# File 'lib/paru/filter/code_block.rb', line 67 def to_file(filename) File.write(filename, "#{@string}\n") end |