Class: String

Inherits:
Object
  • Object
show all
Includes:
ForceArray
Defined in:
lib/liquidoc.rb

Instance Method Summary collapse

Methods included from ForceArray

#force_array, #force_array!

Instance Method Details

#contains_liquid?Boolean

Returns:

  • (Boolean)


1254
1255
1256
1257
1258
1259
1260
1261
# File 'lib/liquidoc.rb', line 1254

def contains_liquid?
  self.each_line do |row|
    if row.match(/.*\{\%.*\%\}.*|.*\{\{.*\}\}.*/)
      return true
    end
  end
  return false
end

#indent(options = {}) ⇒ Object



1247
1248
1249
1250
1251
1252
# File 'lib/liquidoc.rb', line 1247

def indent options = {}
  # TODO: does not allow tabs; inserts explicit `\t` string
  syms = options.fetch(:sym, ' ') * options.fetch(:by, 2)
  self.gsub!(/^/m, "#{syms}")
  self.sub!("#{syms}", "") unless options.fetch(:line1, false)
end

#quote_wrap(options = {}) ⇒ Object



1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
# File 'lib/liquidoc.rb', line 1263

def quote_wrap options = {}
  # When a string contains a certain pattern, wrap it in certain quotes
  # Pass '\s' as pattern to wrap any string that contains 1 or more spaces or tabs
  # pass '.' as pattern to always wrap.

  pattern = options.fetch(:pattern, '\s').to_s
  return self unless self.strip.match(/\s/)
  quotes = options.fetch(:quotes, "single")
  case quotes
  when "single"
    wrap = "''"
  when "double"
    wrap = '""'
  when "backtick"
    wrap = "``"
  when "bracket"
    wrap = "[]"
  else
    wrap = quotes
  end
  quotes << wrap[0] unless wrap[1]
  return wrap[0] + self.strip + wrap[1]
end

#wrap(options = {}) ⇒ Object



1237
1238
1239
1240
1241
1242
1243
1244
1245
# File 'lib/liquidoc.rb', line 1237

def wrap options = {}
  width = options.fetch(:width, 76) # length to wrap at
  pre = options.fetch(:prepend, '') # text to prepend
  app = options.fetch(:append, '') # text to append
  chars = pre.size + app.size
  self.strip.split("\n").collect do |line|
    line.length + chars.size > width ? line.gsub(/(.{1,#{(width - chars)}})(\s+|$)/, "#{pre}\\1#{app}\n") : "#{pre}#{line}#{app}\n"
  end.map(&:rstrip).join("\n")
end