lipsum.rb
By Greg | 25 Aug 2009
Le Lorem Ipsum est simplement du faux texte employé dans la composition et la mise en page avant impression. Le Lorem Ipsum est le faux texte standard de l'imprimerie depuis les années 1500, quand un peintre anonyme assembla ensemble des morceaux de texte pour réaliser un livre spécimen de polices de texte. Il n'a pas fait que survivre cinq siècles, mais s'est aussi adapté à la bureautique informatique, sans que son contenu n'en soit modifié. Il a été popularisé dans les années 1960 grâce à la vente de feuilles Letraset contenant des passages du Lorem Ipsum, et, plus récemment, par son inclusion dans des applications de mise en page de texte, comme Aldus PageMaker. -- lipsum.com
require 'net/http'
require 'rubygems'
#I_KNOW_I_AM_USING_AN_OLD_AND_BUGGY_VERSION_OF_LIBXML2=true
require 'nokogiri'
# This class allow you to retrive "lorem ipsum" placeholder text from lipsum.com.
#
# == What is "lorem ipsum"?
#
# "Lorem Ipsum is simply dummy text of the printing and typesetting industry.
# Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
# when an unknown printer took a galley of type and scrambled it to make a type
# specimen book. It has survived not only five centuries, but also the leap
# into electronic typesetting, remaining essentially unchanged. It was
# popularised in the 1960s with the release of Letraset sheets containing Lorem
# Ipsum passages, and more recently with desktop publishing software like Aldus
# PageMaker including versions of Lorem Ipsum." (from lipsum.com)
#
# == Synopsys
#
# require 'lipsum'
#
# t = Lipsum.new( )
# t.paragraphs[3]
#
# puts t
#
# === Initialization
#
# Lipsum::new( start = true )
#
# If start is set to true, the placeholder text will start with 'Lorem ipsum
# dolor sit amet...'.
#
# === Create paragraphs, words, bytes or lists
#
# Lipsum.paragraphs[size]
# Lipsum.words[size]
# Lipsum.bytes[size]
# Lipsum.lists[size]
#
# With those methods, you can create placeholder texts with size `size'
#
# === Print
#
# Lipsum.to_s
# Lipsum.to_html
#
# You can just 'puts' the Lipsum instance to print the text version. If you want
# an html version, use Lipsum.to_html
#
# == Requirements
#
# This lib require nokogiri : http://nokogiri.rubyforge.org/nokogiri/
#
# == Licence
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY
without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
class Lipsum
class AllTypes #:nodoc:
def initialize(start)
@amount = 1
@what = nil
@start = start
end
def [](n)
@amount = n
generate
self
end
def to_s
r = ""
Nokogiri::XML.parse(@lorem_ipsum.to_s).xpath('/div[@id="lipsum"]/p').each do |p|
r << p.content.to_s.strip << "\n\n"
end
r
end
alias_method :to_string, :to_s
def to_html
Nokogiri::XML.parse(@lorem_ipsum.to_s).xpath('/div[@id="lipsum"]/p').to_s
end
private
def generate
opts = {'amount' => @amount, 'what' => @what}
opts['start'] = 1 if @start
response = Net::HTTP.post_form(URI.parse('http://lipsum.com/feed/html'), opts)
html = Nokogiri::HTML.parse(response.body)
@lorem_ipsum = html.xpath('//div[@id="lipsum"]')
end
end
class Paragraphs < AllTypes #:nodoc:
def initialize(start)
super(start)
@what = "para"
end
end
class Words < AllTypes #:nodoc:
def initialize(start)
super(start)
@what = "words"
end
end
class Bytes < AllTypes #:nodoc:
def initialize(start)
super(start)
@what = "bytes"
end
end
class Lists < AllTypes #:nodoc:
def initialize(start)
super(start)
@what = "lists"
end
def to_s
r = ""
Nokogiri::XML.parse(@lorem_ipsum.to_s).xpath('/div[@id="lipsum"]/ul').each do |ul|
r << ul.content.to_s.strip.split(/\n/).map { |x| "* " << x }.join( "\n" )
r << "\n\n"
end
r
end
def to_html
Nokogiri::XML.parse(@lorem_ipsum.to_s).xpath('/div[@id="lipsum"]/ul').to_s
end
end
def initialize(start = true)
@start = start
end
# Create paragraphs
def paragraphs
@lipsum = Paragraphs.new(@start)
end
# Create words
def words
@lipsum = Words.new(@start)
end
# Create bytes
def bytes
@lipsum = Bytes.new(@start)
end
# Create lists
def lists
@lipsum = Lists.new(@start)
end
# Retrieve text
def to_s
@lipsum.to_s
end
alias_method :to_string, :to_s
# Retrieve test in HTML format
def to_html
@lipsum.to_html
end
end
if __FILE__ == $0
t = Lipsum.new(true)
t.paragraphs[4]
puts "TEXT :"
puts t
puts "HTML :"
puts t.to_html
end