Class: Kramdown::AsciiDoc::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/kramdown-asciidoc/writer.rb

Constant Summary collapse

LF =
?\n

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWriter

Returns a new instance of Writer.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/kramdown-asciidoc/writer.rb', line 11

def initialize
  @prologue = []
  @doctitle = nil
  @attributes = {}
  @body = []
  @nesting_stack = []
  @block_delimiter = nil
  @block_separator = ['']
  @list_level = { list: 0, dlist: 0 }
  @follows_list = false
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



9
10
11
# File 'lib/kramdown-asciidoc/writer.rb', line 9

def body
  @body
end

#doctitleObject

Returns the value of attribute doctitle.



8
9
10
# File 'lib/kramdown-asciidoc/writer.rb', line 8

def doctitle
  @doctitle
end

Instance Method Details

#add_attributes(new_attributes) ⇒ Object



23
24
25
# File 'lib/kramdown-asciidoc/writer.rb', line 23

def add_attributes new_attributes
  @attributes.update new_attributes
end

#add_blank_lineObject



84
85
86
87
# File 'lib/kramdown-asciidoc/writer.rb', line 84

def add_blank_line
  @body << ''
  nil
end

#add_line(line) ⇒ Object



89
90
91
92
# File 'lib/kramdown-asciidoc/writer.rb', line 89

def add_line line
  @body << line
  nil
end

#add_lines(lines) ⇒ Object



94
95
96
97
# File 'lib/kramdown-asciidoc/writer.rb', line 94

def add_lines lines
  @body += lines
  nil
end

#add_prologue_line(line) ⇒ Object



27
28
29
# File 'lib/kramdown-asciidoc/writer.rb', line 27

def add_prologue_line line
  @prologue << line
end

#add_prologue_lines(lines) ⇒ Object



31
32
33
# File 'lib/kramdown-asciidoc/writer.rb', line 31

def add_prologue_lines lines
  @prologue.push(*lines)
end

#append(str) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/kramdown-asciidoc/writer.rb', line 99

def append str
  if empty?
    @body << str
  else
    @body[-1] += str
  end
  nil
end

#clear_lineObject



108
109
110
# File 'lib/kramdown-asciidoc/writer.rb', line 108

def clear_line
  replace_line ''
end

#current_lineObject



118
119
120
# File 'lib/kramdown-asciidoc/writer.rb', line 118

def current_line
  @body[-1]
end

#empty?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/kramdown-asciidoc/writer.rb', line 122

def empty?
  @body.empty?
end

#end_delimited_blockObject



50
51
52
53
54
# File 'lib/kramdown-asciidoc/writer.rb', line 50

def end_delimited_block
  parent_body, @block_delimiter, @block_separator, @list_level = @nesting_stack.pop
  @body = (parent_body + @body) << @block_delimiter
  @block_delimiter = nil
end

#end_list(kin) ⇒ Object



65
66
67
68
69
70
# File 'lib/kramdown-asciidoc/writer.rb', line 65

def end_list kin
  @follows_list = true
  @block_separator.pop
  @list_level[kin] -= 1
  nil
end

#follows_list?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/kramdown-asciidoc/writer.rb', line 80

def follows_list?
  @follows_list
end

#in_list?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/kramdown-asciidoc/writer.rb', line 76

def in_list?
  @block_separator[-1] == '+'
end

#list_level(kin = :list) ⇒ Object



72
73
74
# File 'lib/kramdown-asciidoc/writer.rb', line 72

def list_level kin = :list
  @list_level[kin]
end

#replace_line(line) ⇒ Object



112
113
114
115
116
# File 'lib/kramdown-asciidoc/writer.rb', line 112

def replace_line line
  @body.pop
  @body << line
  nil
end

#start_blockObject



35
36
37
38
39
# File 'lib/kramdown-asciidoc/writer.rb', line 35

def start_block
  @follows_list = false
  @body << @block_separator[-1] unless empty?
  nil
end

#start_delimited_block(delimiter) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/kramdown-asciidoc/writer.rb', line 41

def start_delimited_block delimiter
  @follows_list = false
  @body << (@block_delimiter = delimiter.length == 1 ? delimiter * 4 : delimiter)
  @nesting_stack << [(@body.pop @body.length), @block_delimiter, @block_separator, @list_level]
  @block_separator = ['']
  @list_level = { list: 0, dlist: 0 }
  nil
end

#start_list(compound, kin) ⇒ Object

Q: perhaps do_in_list that takes a block?



57
58
59
60
61
62
63
# File 'lib/kramdown-asciidoc/writer.rb', line 57

def start_list compound, kin
  # Q: can this be further optimized?
  @body << '' if in_list? ? compound : !empty?
  @block_separator << '+'
  @list_level[kin] += 1
  nil
end

#to_sObject



126
127
128
129
130
131
132
133
# File 'lib/kramdown-asciidoc/writer.rb', line 126

def to_s
  header = @prologue.dup
  header << %(= #{@doctitle}) if @doctitle
  @attributes.sort.each do |name, val|
    header << (val.empty? ? %(:#{name}:) : %(:#{name}: #{val}))
  end unless @attributes.empty?
  (header.empty? ? @body : (header + (@body.empty? ? [] : [''] + @body))).join LF
end