Class: RandomWords::LoremHTML

Inherits:
Object
  • Object
show all
Defined in:
lib/random-words/lorem_html.rb

Overview

Generates random Lorem Ipsum text in Markdown format.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ String

Generates random Lorem Ipsum text.

Options Hash (options):

  • :source (Symbol)

    The source of the words (:latin, :english, :french, etc.).

  • :grafs (Integer)

    The number of paragraphs to generate.

  • :sentences (Integer)

    The number of sentences per paragraph.

  • :length (Symbol)

    The length of the text (:short, :medium, :long, :very_long).

  • :decorate (Boolean)

    Whether to include emphasis and strong tags.

  • :link (Boolean)

    Whether to include links.

  • :ul (Boolean)

    Whether to include unordered lists.

  • :ol (Boolean)

    Whether to include ordered lists.

  • :dl (Boolean)

    Whether to include definition lists.

  • :bq (Boolean)

    Whether to include blockquotes.

  • :code (Boolean)

    Whether to include code blocks.

  • :mark (Boolean)

    Whether to include mark tags.

  • :headers (Boolean)

    Whether to include headers.

  • :table (Boolean)

    Whether to include tables.

  • :extended (Boolean)

    Whether to use extended punctuation.



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
# File 'lib/random-words/lorem_html.rb', line 34

def initialize(options = {})
  @added = {
    italic: false,
    strong: false,
    link: false,
    code: false,
    mark: false,
    footnote: false,
    hr: false
  }

  defaults = {
    source: :latin,
    grafs: 10,
    sentences: 5,
    length: :medium,
    decorate: true,
    link: false,
    ul: false,
    ol: false,
    dl: false,
    bq: false,
    code: false,
    mark: false,
    headers: false,
    table: false,
    extended: false,
    footnote: false,
    hr: false,
    image: false
  }

  @options = defaults.merge(options)

  @generator = RandomWords::Generator.new(@options[:source], {
                                            sentence_length: @options[:length],
                                            paragraph_length: @options[:sentences],
                                            use_extended_punctuation: @options[:extended]
                                          })
  @title = fragment(2, 5).cap_first

  @output = ''
  strong = @options[:decorate]
  italic = @options[:decorate]
  links = @options[:link]
  code = @options[:code]
  mark = @options[:mark]
  footnotes = @options[:footnote]
  @options[:hr]
  @options[:image]
  force = []
  # Generate the specified number of paragraphs.
  @options[:grafs].times.with_index do |_, i|
    if @options[:grafs] == 1 || i == @options[:grafs] - 2
      force << :footnote if !@added[:footnote] && @options[:footnote]
      force << :code if !@added[:code] && @options[:code]
      force << :link if !@added[:link] && @options[:link]
      force << :italic if !@added[:italic] && @options[:decorate]
      force << :strong if !@added[:strong] && @options[:decorate]
    end

    @output += paragraph(1, italic: italic, strong: strong, links: links, code: code, mark: mark, footnotes: footnotes,
                            force: force)
    italic = Random.rand(0..3).zero? && @options[:decorate]
    strong = Random.rand(0..3).zero? && @options[:decorate]
    links = Random.rand(0..3).zero? && @options[:link]
    code = Random.rand(0..3).zero? && @options[:code]
    footnotes = Random.rand(0..3).zero? && @options[:footnote]

    @output += "\n\n"
  end

  generate
end

Instance Attribute Details

#generatorObject

Stores the RandomWords::Generator



10
11
12
# File 'lib/random-words/lorem_html.rb', line 10

def generator
  @generator
end

#outputObject

Stores the output



7
8
9
# File 'lib/random-words/lorem_html.rb', line 7

def output
  @output
end

#titleObject (readonly)

Stores the title



13
14
15
# File 'lib/random-words/lorem_html.rb', line 13

def title
  @title
end

Instance Method Details

#generateString

Use the RandomWords class to generate the Lorem Ipsum text.



112
113
114
115
116
117
118
119
# File 'lib/random-words/lorem_html.rb', line 112

def generate
  compress_newlines
  handle_lists
  handle_basic_elements
  handle_content_blocks
  finalize_output
  @output
end