Sha256: 2dc5dbb75cfacbcfdb17ac7ea0aa0850f46d309dfd534d64586859642f8c0ddb

Contents?: true

Size: 1.58 KB

Versions: 19

Compression:

Stored size: 1.58 KB

Contents

# Global text helpers
#
module.exports =

  # Whitespace helper function.
  #
  # @param [Number] n the number if indents
  # @return [String] the indention string
  #
  whitespace: (n) ->
    n = n * 2
    a = []
    while a.length < n
      a.push ' '
    a.join ''

  # Escape quotation in a text.
  #
  # @param [String] text the text containing quotes
  # @return [String] the escaped text
  #
  escapeQuotes: (text) ->
    return '' unless text

    text.replace(/"/g, '\\"').replace(/\\\\\"/g, '\\"')

  # Unescape quotation in a text.
  #
  # @param [String] text the text containing escaped quotes
  # @return [String] the text without escaped quotes
  #
  unescapeQuotes: (text) ->
    return '' unless text

    text.replace(/\\"/g, '"')

  # HTML Escape a text.
  #
  # @param [String] text the text
  # @return [String] the HTML escaped text
  #
  escapeHTML: (text) ->
    return '' unless text

    text
      .replace(/&/g, '&amp;')
      .replace(/</g, '&lt;')
      .replace(/>/g, '&gt;')
      .replace(/\"/g, '&quot;')

  # Preserve newlines within the preserve tags,
  #
  # @param [String] code the code to preserve
  # @return [String] the preserved code
  #
  preserve: (code) ->
    if code
      code.replace(/\r/g, '')
      code.replace /<(pre|textarea)>(.*?)<\/\1>/g, (text) ->
        text.replace('\\n', '\&\#x000A;')

  # Indent the given text.
  #
  # @param [String] text the text to indent
  # @param [Integer] the amount of spaced to insert
  # @return [String] the indented text
  #
  indent: (text, spaces) ->
    text.replace /^(.*)$/mg, module.exports.whitespace(spaces) + '$1'

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
ela-4.1.6 node_modules/haml-coffee/src/util/text.coffee
ela-4.1.5 node_modules/haml-coffee/src/util/text.coffee
ela-4.1.4 node_modules/haml-coffee/src/util/text.coffee
ela-4.1.3 node_modules/haml-coffee/src/util/text.coffee
ela-4.1.2 node_modules/haml-coffee/src/util/text.coffee
ela-4.1.1 node_modules/haml-coffee/src/util/text.coffee
ela-4.1.0 node_modules/haml-coffee/src/util/text.coffee
ela-4.0.0 node_modules/haml-coffee/src/util/text.coffee
ela-3.4.3 node_modules/haml-coffee/src/util/text.coffee
ela-3.4.2 node_modules/haml-coffee/src/util/text.coffee
ela-3.4.0 node_modules/haml-coffee/src/util/text.coffee
ela-3.3.1 node_modules/haml-coffee/src/util/text.coffee
ela-3.3.0 node_modules/haml-coffee/src/util/text.coffee
ela-3.2.0 node_modules/haml-coffee/src/util/text.coffee
ela-3.1.1 node_modules/haml-coffee/src/util/text.coffee
ela-3.1.0 node_modules/haml-coffee/src/util/text.coffee
ela-3.0.0 node_modules/haml-coffee/src/util/text.coffee
ela-2.0.0 node_modules/haml-coffee/src/util/text.coffee
ela-1.1.0 node_modules/haml-coffee/src/util/text.coffee