File: html_ipsum.rb

package info (click to toggle)
ruby-ffaker 2.23.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,776 kB
  • sloc: ruby: 12,788; makefile: 8; sh: 1
file content (186 lines) | stat: -rw-r--r-- 5,086 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# frozen_string_literal: true

module FFaker
  # Loosely based on http://html-ipsum.com/
  # Requires FFaker::Lorem module
  module HTMLIpsum
    extend ModuleUtils
    extend self

    def a(word_count = 2)
      "<a href=\"##{word}\" title=\"#{words(word_count).capitalize!}\">#{words(word_count).capitalize!}</a>"
    end

    def p(count = 3, options = {})
      options = { fancy: false, include_breaks: false }.merge(options)
      if options[:fancy]
        s = fancy_string(count, options[:include_breaks])
      else
        mode = options[:include_breaks] ? 'paragraphs' : 'paragraph'
        s = send(mode.to_sym, count)
      end
      content_tag_for(:p, s)
    end

    def dl(definitions = 2)
      content_tag_for :dl do |dl|
        definitions.times do
          dl << content_tag_for(:dt, words(1).capitalize!)
          dl << content_tag_for(:dd, paragraph(2))
        end
      end
    end

    def ul_short(items = 3)
      content_tag_for :ul do |ul|
        items.times do
          ul << content_tag_for(:li, sentence(2))
        end
      end
    end

    def ul_long(items = 3)
      content_tag_for :ul do |ul|
        items.times do
          ul << content_tag_for(:li, paragraph(2))
        end
      end
    end

    def ol_short(items = 3)
      content_tag_for :ol do |ol|
        items.times do
          ol << content_tag_for(:li, sentence(2))
        end
      end
    end

    def ol_long(items = 3)
      content_tag_for :ol do |ol|
        items.times do
          ol << content_tag_for(:li, paragraph(2))
        end
      end
    end

    def ul_links(items = 3)
      content_tag_for :ul do |ul|
        items.times do
          ul << content_tag_for(:li, a(1))
        end
      end
    end

    def table(rows = 3)
      content_tag_for(:table) do |table|
        table << content_tag_for(:thead) do |thead|
          thead << content_tag_for(:tr) do |tr|
            tr << content_tag_for(:th, word.capitalize)
            tr << content_tag_for(:th, word.capitalize)
            tr << content_tag_for(:th, word.capitalize)
            tr << content_tag_for(:th, word.capitalize)
          end
        end
        table << content_tag_for(:tbody) do |tbody|
          rows.times do
            tbody << content_tag_for(:tr) do |tr|
              tr << content_tag_for(:td, words(1).capitalize)
              tr << content_tag_for(:td, words(1).capitalize)
              tr << content_tag_for(:td, words(1).capitalize)
              tr << content_tag_for(:td, a)
            end
          end
        end
      end
    end

    def body
      body = content_tag_for(:h1, words(2).capitalize)
      rand(0..3).times do
        body << content_tag_for(:p, fancy_string)
      end
      body << table(rand(0..3))
      body << content_tag_for(:h2, words(2).capitalize)
      body << content_tag_for(:ol) do |ol|
        rand(0..3).times do
          ol << content_tag_for(:li, paragraph(1))
        end
      end
      body << content_tag_for(:blockquote) do |bq|
        bq << content_tag_for(:p, paragraphs(3))
      end
      body << content_tag_for(:h3, words(2).capitalize!)
      body << content_tag_for(:ul) do |ul|
        rand(0..3).times do
          ul << content_tag_for(:li, paragraph(1))
        end
      end
      body << content_tag_for(:pre) do |pre|
        pre << content_tag_for(:code) do |code|
          code << "
            ##{word} h1 a {
              display: block;
              width: 300px;
              height: 80px;
            }
          "
        end
      end
      body
    end

    def fancy_string(count = 3, include_breaks = false)
      sep = include_breaks ? '<br>' : ' '
      a = k([
        content_tag_for(:strong, words(2).capitalize!),
        content_tag_for(:em, paragraph),
        content_tag_for(:mark, paragraph),
        content_tag_for(:del, words(2)),
        content_tag_for(:ins, words(2)),
        content_tag_for(:sub, words(2)),
        content_tag_for(:sup, words(2)),
        content_tag_for(:code, words(2)),
        content_tag_for(:small, words(2)),
        (a 2).to_s
      ] + FFaker::Lorem.paragraphs(count - 1))
      fetch_sample(a, count: count).join(sep)
    end

    private

    def content_tag_for(element, content = nil)
      element_content = if content
                          content
                        else
                          block_html = +''
                          yield(block_html)
                          block_html
                        end
      +"<#{element}>#{element_content}</#{element}>"
    end

    def word
      FFaker::Lorem.word
    end

    def words(word_count = 3)
      FFaker::Lorem.words(word_count).join(' ')
    end

    def sentence(word_count = 3)
      FFaker::Lorem.sentence(word_count)
    end

    def sentences(sentence_count = 3)
      FFaker::Lorem.sentences(sentence_count).join(' ')
    end

    def paragraph(sentence_count = 3)
      FFaker::Lorem.paragraph(sentence_count)
    end

    def paragraphs(paragraph_count = 3)
      FFaker::Lorem.paragraphs(paragraph_count).join('<br>')
    end
  end
end