Class: Md2Cnblog::BaseTranslator

Inherits:
Object
  • Object
show all
Defined in:
lib/md2cnblog/base_translator.rb

Direct Known Subclasses

CnblogTranslator, IteyeTranslator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, options = {}, render = nil) ⇒ BaseTranslator

Returns a new instance of BaseTranslator.



6
7
8
9
10
11
# File 'lib/md2cnblog/base_translator.rb', line 6

def initialize(str, options={}, render=nil)
  @str ||= str if valide_str?(str)
  @options = default_options.merge(options) if valide_option?(options)
  @render = render || default_render
  @markdown = ::Redcarpet::Markdown.new(@render, @options)
end

Instance Attribute Details

#markdownObject (readonly)

Returns the value of attribute markdown.



3
4
5
# File 'lib/md2cnblog/base_translator.rb', line 3

def markdown
  @markdown
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/md2cnblog/base_translator.rb', line 3

def options
  @options
end

#outputObject (readonly)

Returns the value of attribute output.



3
4
5
# File 'lib/md2cnblog/base_translator.rb', line 3

def output
  @output
end

#renderObject

Returns the value of attribute render.



4
5
6
# File 'lib/md2cnblog/base_translator.rb', line 4

def render
  @render
end

Instance Method Details

#default_optionsObject



23
24
25
26
27
28
# File 'lib/md2cnblog/base_translator.rb', line 23

def default_options
  { no_intra_emphasis: true,
    autolink: true, 
    space_after_headers: true,
    fenced_code_blocks: true }
end

#default_renderObject



30
31
32
# File 'lib/md2cnblog/base_translator.rb', line 30

def default_render
  Redcarpet::Render::HTML.new()
end

#echoObject



43
44
45
# File 'lib/md2cnblog/base_translator.rb', line 43

def echo
  print @output
end

#startObject



34
35
36
37
# File 'lib/md2cnblog/base_translator.rb', line 34

def start
  start_mute
  echo 
end

#start_muteObject



39
40
41
# File 'lib/md2cnblog/base_translator.rb', line 39

def start_mute
  @output = @markdown.render(@str)
end

#valide_option?(options) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



18
19
20
21
# File 'lib/md2cnblog/base_translator.rb', line 18

def valide_option?(options)
  raise InvalidOptionsError unless options.is_a?(Hash) 
  true
end

#valide_str?(str) ⇒ Boolean

Returns:

  • (Boolean)

Raises:



13
14
15
16
# File 'lib/md2cnblog/base_translator.rb', line 13

def valide_str?(str)
  raise InvalidInputError unless str.is_a?(String) 
  true
end