Module: NanDoc::StringFormatting

Included in:
Filters::General, Helpers::NanDocHelpers, Helpers::NanDocHelpers::MenuBouncy, OptsNormalizer::OptEnum, Nanoc3::Item
Defined in:
lib/nandoc/support-modules.rb,
lib/nandoc/support-modules.rb

Instance Method Summary collapse

Instance Method Details

#basename_no_extension(str) ⇒ Object



158
159
160
# File 'lib/nandoc/support-modules.rb', line 158

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

#indent(str, indent) ⇒ Object



161
162
163
# File 'lib/nandoc/support-modules.rb', line 161

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

#no_blank_lines(str) ⇒ Object



164
165
166
# File 'lib/nandoc/support-modules.rb', line 164

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

#no_leading_ws(str) ⇒ Object



167
168
169
# File 'lib/nandoc/support-modules.rb', line 167

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

#no_trailing_ws(str) ⇒ Object



170
171
172
# File 'lib/nandoc/support-modules.rb', line 170

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

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



173
174
175
176
177
178
179
180
# File 'lib/nandoc/support-modules.rb', line 173

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

#quotedObject



181
182
183
# File 'lib/nandoc/support-modules.rb', line 181

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



192
193
194
195
196
197
198
199
200
201
# File 'lib/nandoc/support-modules.rb', line 192

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) ⇒ Object



203
204
205
# File 'lib/nandoc/support-modules.rb', line 203

def unindent str, by
  str.gsub(/^#{Regexp.escape(by)}/, '')
end