Class: Koios::Doc

Inherits:
Object
  • Object
show all
Defined in:
lib/koios/doc.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDoc

Returns a new instance of Doc.



79
80
81
# File 'lib/koios/doc.rb', line 79

def initialize
  @content = []
end

Class Method Details

.write(&content) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/koios/doc.rb', line 3

def self.write(&content)
  instance = self.new

  instance.extend_string
  String.koios = instance

  instance.content = instance.instance_eval(&content)

  instance.unextend_string

  instance.to_s
end

Instance Method Details

#a(url, content = nil) ⇒ Object



48
49
50
51
# File 'lib/koios/doc.rb', line 48

def a(url, content = nil)
  c = content || url
  url.link_to c
end

#code(*args) ⇒ Object



57
58
59
# File 'lib/koios/doc.rb', line 57

def code(*args)
  "\n```\n" + args.join("\n") + "\n```"
end

#code_for(lang, *args) ⇒ Object



61
62
63
# File 'lib/koios/doc.rb', line 61

def code_for(lang, *args)
  "\n```#{lang}\n" + args.join("\n") + "\n```"
end

#content=(arr) ⇒ Object



179
180
181
# File 'lib/koios/doc.rb', line 179

def content=(arr)
  @content = arr
end

#extend_stringObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/koios/doc.rb', line 83

def extend_string
  String.class_eval do
    class << self
      def koios=(koios)
        @@koios = koios
      end
    end

    def h1
      h(self, 1)
    end

    def h2
      h(self, 2)
    end

    def h3
      h(self, 3)
    end

    def h4
      h(self, 4)
    end

    def h5
      h(self, 5)
    end

    def h6
      h(self, 6)
    end

    def break_line
      self + "  \n"
    end

    def link_to(content = nil)
      c = content || self
      "[#{c}](#{self})"
    end

    def img(alt = nil)
      label = alt || ""
      "![#{label}](#{self})"
    end

    def italic
      "_#{self}_"
    end

    def bold
      "**#{self}**"
    end

    def strikethrough
      "~~#{self}~~"
    end

    def code
      "`#{self}`"
    end

    def pre
      self.split("\n").map { |l| "    #{l}" }.join("\n")
    end

    private
    def h(content, level)
      "\n#{"#" * level} #{content}"
    end
  end
end

#h1(*args) ⇒ Object



24
25
26
# File 'lib/koios/doc.rb', line 24

def h1(*args)
  args.join('').h1
end

#h2(*args) ⇒ Object



28
29
30
# File 'lib/koios/doc.rb', line 28

def h2(*args)
  args.join('').h2
end

#h3(*args) ⇒ Object



32
33
34
# File 'lib/koios/doc.rb', line 32

def h3(*args)
  args.join('').h3
end

#h4(*args) ⇒ Object



36
37
38
# File 'lib/koios/doc.rb', line 36

def h4(*args)
  args.join('').h4
end

#h5(*args) ⇒ Object



40
41
42
# File 'lib/koios/doc.rb', line 40

def h5(*args)
  args.join('').h5
end

#h6(*args) ⇒ Object



44
45
46
# File 'lib/koios/doc.rb', line 44

def h6(*args)
  args.join('').h6
end

#img(url, alt = nil) ⇒ Object



53
54
55
# File 'lib/koios/doc.rb', line 53

def img(url, alt = nil)
  url.img alt
end

#ol(items) ⇒ Object



70
71
72
73
# File 'lib/koios/doc.rb', line 70

def ol(items)
  list = items.map.with_index { |item, i| "#{i + 1}. #{item}" }.join("\n")
  "\n#{list}"
end

#p(*args) ⇒ Object



20
21
22
# File 'lib/koios/doc.rb', line 20

def p(*args)
  "\n" + args.join('')
end

#pre(*args) ⇒ Object



75
76
77
# File 'lib/koios/doc.rb', line 75

def pre(*args)
  args.join("\n").pre
end

#to_sObject



16
17
18
# File 'lib/koios/doc.rb', line 16

def to_s
  @content.join("\n") + "\n"
end

#ul(items) ⇒ Object



65
66
67
68
# File 'lib/koios/doc.rb', line 65

def ul(items)
  list = items.map { |item| "- #{item}" }.join("\n")
  "\n#{list}"
end

#unextend_stringObject



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/koios/doc.rb', line 156

def unextend_string
  String.class_eval do
    remove_method :h1
    remove_method :h2
    remove_method :h3
    remove_method :h4
    remove_method :h5
    remove_method :h6
    remove_method :break_line
    remove_method :link_to
    remove_method :img
    remove_method :italic
    remove_method :bold
    remove_method :strikethrough
    remove_method :code
    remove_method :pre

    class << self
      remove_method :koios=
    end
  end
end