Class: Paru::PandocFilter::CodeBlock

Inherits:
Block
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(contents) ⇒ CodeBlock

Create a new CodeBlock based on the contents

Parameters:

  • contents (Array)

    an array with the attribute and the code string



44
45
46
47
# File 'lib/paru/filter/code_block.rb', line 44

def initialize(contents)
  @attr = Attr.new contents[0]
  @string = contents[1]
end

Instance Attribute Details

#attrAttr

Returns:



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

#stringString

Returns:

  • (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

Parameters:

  • code_string (String)

    the string with code to use as the contents of the CodeBlock

  • language (String) (defaults to: '')

    the optional language class

Returns:



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

Parameters:

  • filename (String)

    the path to the file to read the contents from

  • language (String) (defaults to: '')

    the language of the contents

Returns:



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_contentsObject

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?

Returns:

  • (Boolean)

    true



60
61
62
# File 'lib/paru/filter/code_block.rb', line 60

def has_string?
  true
end

#inner_markdownString Originally defined in module InnerMarkdown

Get the markdown representation of this Node’s children.

Examples:

Replace all occurrences of “hello” by “world” in all paragraphs

Paru::Filter.run do
    with "Para" do |p|
        p.inner_markdown = p.inner_markdown.gsub "hello", "world"
    end
end

Returns:

  • (String)

    the inner markdown representation of this Node

#inner_markdown=(markdown) ⇒ Object Originally defined in module InnerMarkdown

Replace this Node’s children with the Nodes represented by the markdown string

Examples:

Replace all occurrences of “hello” by “world” in all paragraphs

Paru::Filter.run do
    with "Para" do |p|
        p.inner_markdown = p.inner_markdown.gsub "hello", "world"
    end
end

Parameters:

  • markdown (String)

    the markdown string to replace this Node’s children

#to_code_stringString

Get this CodeBlock’s contents as a string

Returns:

  • (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

Parameters:

  • filename (String)

    the path to the file to write



67
68
69
# File 'lib/paru/filter/code_block.rb', line 67

def to_file(filename)
  File.write(filename, "#{@string}\n")
end