Module: NanDoc::StringMethods

Included in:
Cli::OptionMethods::OptionEnum, Helpers::NanDocHelpers, Helpers::NanDocHelpers::MenuBouncy, Nanoc3::Item
Defined in:
lib/nandoc/support/string-methods.rb

Overview

@dependencies: none

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.oxford_comma(items, final = ' and ', &quoter) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/nandoc/support/string-methods.rb', line 20

def oxford_comma items, final = ' and ', &quoter
  items = items.map(&quoter) if quoter
  these = []
  these.push final if items.size > 1
  these.concat(Array.new(items.size-2,', ')) if items.size > 2
  these.reverse!
  items.zip(these).flatten.compact.join
end

Instance Method Details

#basename_no_extension(str) ⇒ Object



5
6
7
# File 'lib/nandoc/support/string-methods.rb', line 5

def basename_no_extension str
  /([^\/\.]+)(?:\.[^\.\/]+)?\Z/ =~ str ? $1 : nil
end

#indent(str, indent) ⇒ Object



8
9
10
# File 'lib/nandoc/support/string-methods.rb', line 8

def indent str, indent
  str.gsub(/^/, indent)
end

#no_blank_lines(str) ⇒ Object



11
12
13
# File 'lib/nandoc/support/string-methods.rb', line 11

def no_blank_lines str
  str.gsub(/\n[[:space:]]*\n/, "\n")
end

#no_leading_ws(str) ⇒ Object



14
15
16
# File 'lib/nandoc/support/string-methods.rb', line 14

def no_leading_ws str
  str.sub(/\A[[:space:]]+/, '')
end

#no_trailing_ws(str) ⇒ Object



17
18
19
# File 'lib/nandoc/support/string-methods.rb', line 17

def no_trailing_ws str
  str.sub(/[[:space:]]+\Z/, '')
end

#quotedObject



29
30
31
# File 'lib/nandoc/support/string-methods.rb', line 29

def quoted
  proc{|x| "\"#{x}\"" }
end

#reindent(h1, offset = 0) ⇒ Object

must respond to tab() and tabs() reindent a block by striping leading whitespace from lines evenly and then re-indenting each line according to our indent. this could be simpler, it has been more complicated we do it languidly because we can



40
41
42
43
44
45
46
47
48
49
# File 'lib/nandoc/support/string-methods.rb', line 40

def reindent h1, offset=0
  indent_by = tab * (tabs+offset)
  unindent_by = (/\A([[:space:]]+)/ =~ h1 && $1) or
    fail('regex fail -- not sure if we need this to be so strict')
  h2 = no_blank_lines(h1) # careful. will mess up with <pre> etc
  return h2 if unindent_by == indent_by
  h3 = unindent(h2, unindent_by)
  h4 = indent(h3, indent_by)
  h4
end

#unindent(str, by = nil) ⇒ Object



51
52
53
54
# File 'lib/nandoc/support/string-methods.rb', line 51

def unindent str, by=nil
  by ||= (/\A([ \t]*)/ =~ str and $1 )
  str.gsub(/^#{Regexp.escape(by)}/, '')
end