Sha256: 0ed3a7b1a9dbe3d7d6c4ba148892391f3714429105b1fc718828a8b61485b691

Contents?: true

Size: 1.66 KB

Versions: 34

Compression:

Stored size: 1.66 KB

Contents

class String

  # Adds a newline character unless
  # this string is empty or already ends with a newline
  # See https://unix.stackexchange.com/a/18789
  def ensure_newline
    empty? || ends_with?("\n") ? self : self + "\n"
  end

  def friendlish
    I18n.transliterate(self).
      downcase.
      gsub(/[^0-9a-z ]/, '').
      squish.
      gsub(' ', '-')
  end

  def markdown_paragraphs
    split(/\n\s*\n/)
  end

  def normalize_whitespaces
    gsub(/([^[:ascii:]])/) { $1.blank? ? ' ' : $1 }
  end

  def file_extension
    File.extname(self).delete '.'
  end
end


# The nil-safe affable pipeline goes as follow:
#
# i18n > markdownified > sanitized > affable
#
# Where:
#  * i18n: translates to current locale
#  * markdownified: interpretes markdown in message and generates HTML
#  * sanitized: sanitizes results HTML
#  * affable: changes structure to hide low level details
#
# Other classes may polymorphically implement their own
# markdownified, sanitized and affable methods with similar semantics
# to extend this pipeline to non-strings
class String

  # Creates a humman representation - but not necessary UI - representation
  # of this string by interpreting its markdown as a one-liner and sanitizing it
  def affable
    markdownified(one_liner: true).sanitized
  end

  # Interprets the markdown on this string, and converts it into HTML
  def markdownified(**options)
    Mumukit::ContentType::Markdown.to_html self, options
  end

  # Sanitizes this string, escaping unsafe HTML sequences
  def sanitized
    Mumukit::ContentType::Sanitizer.sanitize self
  end
end

class NilClass
  def affable
  end

  def markdownified(**options)
  end

  def sanitized
  end
end

Version data entries

34 entries across 34 versions & 2 rubygems

Version Path
mumuki-domain-8.1.2 lib/mumuki/domain/extensions/string.rb
mumuki-domain-8.1.1 lib/mumuki/domain/extensions/string.rb
mumuki-domain-8.1.0 lib/mumuki/domain/extensions/string.rb
mumuki-domain-8.0.0 lib/mumuki/domain/extensions/string.rb
mumuki-domain-7.12.2 lib/mumuki/domain/extensions/string.rb
mumuki-domain-7.12.1 lib/mumuki/domain/extensions/string.rb
mumuki-domain-7.12.0 lib/mumuki/domain/extensions/string.rb
mumuki-domain-7.11.1 lib/mumuki/domain/extensions/string.rb
mumuki-laboratory-7.11.1 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-c92f2a7212e2/lib/mumuki/domain/extensions/string.rb
mumuki-laboratory-7.11.0 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-2e15c3330133/lib/mumuki/domain/extensions/string.rb
mumuki-domain-7.11.0 lib/mumuki/domain/extensions/string.rb
mumuki-domain-7.10.1 lib/mumuki/domain/extensions/string.rb
mumuki-domain-7.10.0 lib/mumuki/domain/extensions/string.rb
mumuki-laboratory-7.9.2 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-7082a83493eb/lib/mumuki/domain/extensions/string.rb
mumuki-domain-7.9.2 lib/mumuki/domain/extensions/string.rb
mumuki-laboratory-7.9.1 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-b162dc91aa77/lib/mumuki/domain/extensions/string.rb
mumuki-domain-7.9.1 lib/mumuki/domain/extensions/string.rb
mumuki-laboratory-7.9.0 vendor/bundle/ruby/2.6.0/bundler/gems/mumuki-domain-520f859ca8af/lib/mumuki/domain/extensions/string.rb
mumuki-domain-7.9.0 lib/mumuki/domain/extensions/string.rb
mumuki-domain-7.7.3 lib/mumuki/domain/extensions/string.rb