Class: Paragraph

Inherits:
Object
  • Object
show all
Defined in:
lib/Models/Paragraph.rb

Defined Under Namespace

Classes: CodeBlockMetadata, Iframe, Markup, MetaData, MixtapeMetadata

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json, postID) ⇒ Paragraph

Returns a new instance of Paragraph.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/Models/Paragraph.rb', line 63

def initialize(json, postID)
    @name = json['name']
    @text = json['text']
    @orgText = json['text']
    @type = json['type']
    @href = json['href']
    @postID = postID

    if json['metadata'].nil?
        @metadata = nil
    else
        @metadata = MetaData.new(json['metadata'])
    end

    if json['codeBlockMetadata'].nil?
        @codeBlockMetadata = nil
    else
        @codeBlockMetadata = CodeBlockMetadata.new(json['codeBlockMetadata'])
    end

    if json['mixtapeMetadata'].nil?
        @mixtapeMetadata = nil
    else
        @mixtapeMetadata = MixtapeMetadata.new(json['mixtapeMetadata'])
    end

    if json['iframe'].nil? || !json['iframe'] || !json['iframe']['mediaResource']
        @iframe = nil
    else
        @iframe = Iframe.new(json['iframe']['mediaResource'])
    end
    
    markups = []
    if !json['markups'].nil? && json['markups'].length > 0
        json['markups'].each do |markup|
            markups.append(Markup.new(markup))
        end
        
        links = json['markups'].select{ |markup| markup["type"] == "A" }
        if !links.nil? && links.length > 0
            @markupLinks = links.map{ |link| link["href"] }
        end
    end

    # Walk every char in the paragraph text and inject synthetic ESCAPE
    # markups for chars that would otherwise be re-interpreted as markdown.
    # `precedingChars` is needed by Helper.markdownEscapeNeeded? to detect
    # block-level markers at paragraph start (e.g. `# heading`, `1. item`).
    index = 0
    precedingChars = []
    orgText.each_char do |char|
        if Helper.markdownEscapeNeeded?(char, precedingChars)
            markups.append(Markup.new(
                "type" => 'ESCAPE',
                "start" => index,
                "end" => index + 1
            ))
        end
        precedingChars << char

        index += 1
        if char.bytes.length >= 4
            # some emoji take an extra position in Medium's index space
            index += 1
        end
    end

    @markups = markups
end

Instance Attribute Details

#codeBlockMetadataObject

Returns the value of attribute codeBlockMetadata.



6
7
8
# File 'lib/Models/Paragraph.rb', line 6

def 
  @codeBlockMetadata
end

#hrefObject

Returns the value of attribute href.



6
7
8
# File 'lib/Models/Paragraph.rb', line 6

def href
  @href
end

#iframeObject

Returns the value of attribute iframe.



6
7
8
# File 'lib/Models/Paragraph.rb', line 6

def iframe
  @iframe
end

Returns the value of attribute markupLinks.



6
7
8
# File 'lib/Models/Paragraph.rb', line 6

def markupLinks
  @markupLinks
end

#markupsObject

Returns the value of attribute markups.



6
7
8
# File 'lib/Models/Paragraph.rb', line 6

def markups
  @markups
end

#metadataObject

Returns the value of attribute metadata.



6
7
8
# File 'lib/Models/Paragraph.rb', line 6

def 
  @metadata
end

#mixtapeMetadataObject

Returns the value of attribute mixtapeMetadata.



6
7
8
# File 'lib/Models/Paragraph.rb', line 6

def 
  @mixtapeMetadata
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/Models/Paragraph.rb', line 6

def name
  @name
end

#oliIndexObject

Returns the value of attribute oliIndex.



6
7
8
# File 'lib/Models/Paragraph.rb', line 6

def oliIndex
  @oliIndex
end

#orgTextObject

Returns the value of attribute orgText.



6
7
8
# File 'lib/Models/Paragraph.rb', line 6

def orgText
  @orgText
end

#postIDObject

Returns the value of attribute postID.



6
7
8
# File 'lib/Models/Paragraph.rb', line 6

def postID
  @postID
end

#textObject

Returns the value of attribute text.



6
7
8
# File 'lib/Models/Paragraph.rb', line 6

def text
  @text
end

#typeObject

Returns the value of attribute type.



6
7
8
# File 'lib/Models/Paragraph.rb', line 6

def type
  @type
end

Class Method Details

.makeBlankParagraph(postID) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/Models/Paragraph.rb', line 54

def self.makeBlankParagraph(postID)
    json = {
        "name" => "fakeBlankParagraph_#{SecureRandom.uuid}",
        "text" => "",
        "type" => PParser.getTypeString()
    }
    Paragraph.new(json, postID)
end