Class: PostParser

Inherits:
Object
  • Object
show all
Defined in:
app/services/post_parser.rb

Overview

Parser for post body

Instance Method Summary collapse

Constructor Details

#initialize(entity) ⇒ PostParser

Returns a new instance of PostParser.

Parameters:



6
7
8
9
# File 'app/services/post_parser.rb', line 6

def initialize(entity)
  @entity = entity
  @body   = entity.body.clone
end

Instance Method Details

#convert_asidesObject



33
34
35
36
37
38
# File 'app/services/post_parser.rb', line 33

def convert_asides
  pattern = %r{<p>~~\s*(.+?)</p>}i
  @body.gsub(pattern) do
    "<aside>#{$LAST_MATCH_INFO[1]}</aside>"
  end
end


25
26
27
28
29
30
31
# File 'app/services/post_parser.rb', line 25

def convert_video_links
  pattern = %r(<p>{video:(?<url>[^}]{1,100})}</p>)i
  @body.gsub(pattern) do
    iframe = video_url_to_iframe($LAST_MATCH_INFO[:url])
    %(<div class="proportional-container r-16x9">#{iframe}</div>)
  end
end

#escape_scriptsObject



21
22
23
# File 'app/services/post_parser.rb', line 21

def escape_scripts
  @body.gsub(/<script/, '&lt;script')
end

#parsed_bodyObject



11
12
13
14
15
16
17
18
19
# File 'app/services/post_parser.rb', line 11

def parsed_body
  unless @entity.avoid_parsing?
    @body = escape_scripts
    @body = convert_video_links
    @body = convert_asides
  end

  @body
end