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:

  • entity (Post)


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



30
31
32
33
34
35
# File 'app/services/post_parser.rb', line 30

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


22
23
24
25
26
27
28
# File 'app/services/post_parser.rb', line 22

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



18
19
20
# File 'app/services/post_parser.rb', line 18

def escape_scripts
  @body.gsub(%r{<(/?)script}, '&lt;\1script')
end

#parsed_bodyObject



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

def parsed_body
  @body = escape_scripts
  @body = convert_video_links
  @body = convert_asides
  @body = OembedReceiver.convert(@body)
end