Class: Card::Content

Inherits:
SimpleDelegator
  • Object
show all
Extended by:
Clean
Defined in:
lib/card/content.rb,
lib/card/content/all.rb,
lib/card/content/diff.rb,
lib/card/content/chunk.rb,
lib/card/content/clean.rb,
lib/card/content/parser.rb,
lib/card/content/diff/l_c_s.rb,
lib/card/content/diff/result.rb,
lib/card/content/diff/summary.rb,
lib/card/content/chunk/abstract.rb,
lib/card/content/diff/l_c_s/processor.rb

Overview

Content objects support the parsing of content strings into arrays that contain semantically meaningful “chunks” like nests, links, urls, etc.

Each chunk has an object whose class inherits from Chunk::Abstract

Defined Under Namespace

Modules: All, Chunk, Clean Classes: Diff, Parser

Constant Summary

Constants included from Clean

Clean::ALLOWED_TAGS, Clean::ATTR_VALUE_RE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Clean

clean!

Constructor Details

#initialize(content, format_or_card, opts = {}) ⇒ Content

initialization parses String, detects chunks classes to be used in parsing

Parameters:

  • content (String)
  • format_or_card (Card::Format or Card)
  • opts (Hash) (defaults to: {})

Options Hash (opts):

  • :chunk_list (Symbol)
    • name of registered list of chunk



20
21
22
23
24
25
26
# File 'lib/card/content.rb', line 20

def initialize content, format_or_card, opts={}
  @format = resolve_format format_or_card
  opts ||= {}
  chunk_list = opts[:chunk_list] || @format.chunk_list
  @chunks = Parser.new(chunk_list, self).parse(content)
  super(@chunks.any? ? @chunks : content)
end

Instance Attribute Details

#chunksObject (readonly)

Returns the value of attribute chunks.



12
13
14
# File 'lib/card/content.rb', line 12

def chunks
  @chunks
end

#formatObject (readonly)

Returns the value of attribute format.



12
13
14
# File 'lib/card/content.rb', line 12

def format
  @format
end

#optsObject (readonly)

Returns the value of attribute opts.



12
13
14
# File 'lib/card/content.rb', line 12

def opts
  @opts
end

#revisionObject (readonly)

Returns the value of attribute revision.



12
13
14
# File 'lib/card/content.rb', line 12

def revision
  @revision
end

Instance Method Details

#cardCard

Content must be associated with a Format object, which in turn must be associated with a Card

Returns:



31
32
33
# File 'lib/card/content.rb', line 31

def card
  @format.card
end

#custom_process_chunksObject



54
55
56
57
58
# File 'lib/card/content.rb', line 54

def custom_process_chunks
  each_chunk do |chunk|
    chunk.burn_after_reading yield(chunk)
  end
end

#each_chunkObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/card/content.rb', line 64

def each_chunk
  return enum_for(:each_chunk) unless block_given?

  iterator =
    case __getobj__
    when Hash   then :each_value
    when Array  then :each
    when String then return # no chunks
    else
      Rails.logger.warn "unrecognized type for #each_chunk: " \
                        "#{self.class} #{__getobj__.class}"
      return
    end
  send(iterator) { |item| yield item if item.is_a?(Chunk::Abstract) }
end

#find_chunks(chunk_type = nil) ⇒ Array of Chunk instances

Find all chunks of a given type

Parameters:

  • chunk_type (Chunk Class or Symbol) (defaults to: nil)

Returns:

  • (Array of Chunk instances)


38
39
40
41
# File 'lib/card/content.rb', line 38

def find_chunks chunk_type=nil
  chunk_type = interpret_chunk_type chunk_type
  each_chunk.select { |chunk| chunk.is_a?(chunk_type) }
end

#has_chunk?(chunk_type) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/card/content.rb', line 43

def has_chunk? chunk_type
  each_chunk.any { |chunk| chunk.is_a?(chunk_type) }
end

#inspectObject



95
96
97
# File 'lib/card/content.rb', line 95

def inspect
  "<#{__getobj__.class}:#{card}:#{self}>"
end

#piecesObject



60
61
62
# File 'lib/card/content.rb', line 60

def pieces
  Array.wrap(__getobj__)
end

#process_chunksObject

sends &block to #process_chunk on each Chunk object



48
49
50
51
52
# File 'lib/card/content.rb', line 48

def process_chunks(&)
  return custom_process_chunks(&) if block_given?

  each_chunk(&:process_chunk)
end

#strip_chunks(chunk_type) ⇒ Object



118
119
120
# File 'lib/card/content.rb', line 118

def strip_chunks chunk_type
  chunks.reject! { |c| c.is_a? chunk_type }
end

#strip_nestsObject



114
115
116
# File 'lib/card/content.rb', line 114

def strip_nests
  strip_chunks Chunk::Nest
end

#to_sObject

convert content to String. the common cases here are that either

  • (a) content is already a String, or

  • (b) it’s an Array that needs to be iterated over and converted into a

a string by running to_s on each item.



86
87
88
89
90
91
92
93
# File 'lib/card/content.rb', line 86

def to_s
  case __getobj__
  when Array    then map(&:to_s) * ""
  when String   then __getobj__
  when NilClass then "" # raise "Nil Card::Content"
  else               __getobj__.to_s
  end
end

#without_chunks(*chunk_classes) ⇒ Object



107
108
109
110
111
112
# File 'lib/card/content.rb', line 107

def without_chunks *chunk_classes
  chunk_classes = ::Set.new Array.wrap(chunk_classes)
  stash = stash_chunks chunk_classes
  processed = yield to_s
  unstash_chunks processed, stash
end

#without_nestsObject



99
100
101
# File 'lib/card/content.rb', line 99

def without_nests(&)
  without_chunks(Chunk::Nest, &)
end

#without_referencesObject



103
104
105
# File 'lib/card/content.rb', line 103

def without_references(&)
  without_chunks(Chunk::Nest, Chunk::Link, Chunk::EscapedLiteral, &)
end