Module: MarkdownTitlesToSvg

Defined in:
lib/markdown_titles_to_svg.rb,
lib/markdown_titles_to_svg/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.2"

Class Method Summary collapse

Class Method Details

.generate(markdowns, gh_name, options = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/markdown_titles_to_svg.rb', line 120

def self.generate( markdowns, gh_name, options = {} )
  if self.validate_generate( markdowns, gh_name, options, @TEMPLATE )
    obj = self.options_update( options, @TEMPLATE, 'set_options' )
    obj[:github][:profile] = gh_name
    obj = self.set_fonts( obj )

    cmds = self.markdowns_read( markdowns, obj )

    root = './'
    cmds.each do | cmd |
      svg = self.svg_generate( cmd, obj )
      FileUtils.mkdir_p ( File.dirname( cmd[:path] ) )
      File.open( cmd[:path], 'w' ) { | f | f.write( svg ) }
    end

    obj[:font][:mode].keys.each do | key |
      if obj[:font][:mode][ key ] == :google_fonts
        if ( Time.now.getutc.to_i - obj[:font][:current][ key ].split( '-' )[ 1 ].to_i ) < 30
          if File.basename( obj[:font][:current][ key ] ) .start_with?( key.to_s )
            File.delete( obj[:font][:current][ key ] )
          end
        end
      end
    end
  end

  return true
end

.get_optionsObject



90
91
92
# File 'lib/markdown_titles_to_svg.rb', line 90

def self.get_options()
  return @TEMPLATE
end

.markdown_read(url, obj) ⇒ Object



545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
# File 'lib/markdown_titles_to_svg.rb', line 545

def self.markdown_read( url, obj )
  messages = []
  cmds = []

    uri = URI( url )
    response = Net::HTTP.get( uri )
    doc = Nokogiri::HTML( response )
    doc.xpath( '//img' ).each do | element |
      begin
        if !element.attribute( 'alt' ).nil?
          validation = "#{obj[:github][:source]}#{obj[:github][:profile]}/"
          cmd = {
            headline: nil,
            type: nil,
            src: nil,
            file: nil,
            path: nil
          }
          
          cmd[:src] = element.attribute( 'src' ).value
          cmd[:file] = File.basename( cmd[:src] )

          if cmd[:src].start_with?( validation )
            if cmd[:src].end_with?( '.svg' ) 
              if url =~ URI::regexp
                cmd[:headline] = element.attribute( 'alt' ).value

                cmd[:type] = cmd[:headline].count( '#' ) == 0 ? :default : :h1
                cmd[:headline] = cmd[:headline].gsub( '#', '' ).strip

                tmp = cmd[:src][ cmd[:src].index( validation ) + validation.length, cmd[:src].length ]
                tmp = tmp
                  .split( '/' )
                  .reject { | a | a.empty? }
                  .drop( 2 )
                  .join( '/' )
                cmd[:path] = './' + tmp
                cmds.push( cmd )
              else
                messages.push( "#{cmd[:file]}: Invalid URL Structure." )
              end
            else
              messages.push( "#{cmd[:file]}: Suffix is not valid. Please use .svg instead." )
            end
          else
            messages.push( "#{cmd[:file]}: URL not start with: \"#{validation}\"" )
          end
        end
      rescue
        messages.push( "#{url}: Can not load or a parsing Error occured." )
      end
    end
  return [ cmds, messages ]
end

.single(headline, type, options = {}) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/markdown_titles_to_svg.rb', line 95

def self.single( headline, type, options = {})
  svg = nil
  if self.validate_single( headline, type, options, @TEMPLATE )
    obj = self.options_update( options, @TEMPLATE, 'set_options' )
    obj = self.set_fonts( obj )

    cmd = {}
    cmd[:headline] = headline
    cmd[:type] = type
    svg = self.svg_generate( cmd, obj )
  end

  obj[:font][:mode].keys.each do | key |
    if obj[:font][:mode][ key ] == :google_fonts
      if ( Time.now.getutc.to_i - obj[:font][:current][ key ].split( '-' )[ 1 ].to_i ) < 30
        if File.basename( obj[:font][:current][ key ] ) .start_with?( key.to_s )
          File.delete( obj[:font][:current][ key ] )
        end
      end
    end
  end
  return svg
end

.svg_item(char, color, element, obj, cmd) ⇒ Object



450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/markdown_titles_to_svg.rb', line 450

def self.svg_item( char, color, element, obj, cmd )
  item = {
    "character": nil,
    "fill": nil,
    "fill-opacity": nil,
    "stroke": nil,
    "stroke-width": nil,
    "stroke-linecap": nil,
    "stroke-opacity": nil,
    "transform": nil,
    "d": nil
  }

  item[:"character"] = char
  item[:"fill"] = color
  item[:"fill-opacity"] = obj[:style][ cmd[:type] ][:color][:opacity]
  item[:"stroke"] = obj[:style][ cmd[:type] ][:stroke][:color]
  item[:"stroke-width"] = obj[:style][ cmd[:type] ][:stroke][:width]
  item[:"stroke-linecap"] = obj[:style][ cmd[:type] ][:stroke][:linecap]
  item[:"stroke-opacity"] = obj[:style][ cmd[:type] ][:stroke][:opacity]
  
  [ :"transform", :"d" ].each do | key |
    search = key.to_s
    doc = Nokogiri::XML( element )
    item[ key ] = doc.xpath("//*[@#{search}]")[ 0 ].attribute( search ).to_s
  end

  return item
end

.unzip_file(file, destination, file_name) ⇒ Object



199
200
201
202
203
204
205
206
207
# File 'lib/markdown_titles_to_svg.rb', line 199

def self.unzip_file( file, destination, file_name )
  Zip::ZipFile.open( file ) do | zip_file |
   zip_file.each do | f |
     f_path=File.join( destination, file_name )  
     FileUtils.mkdir_p( File.dirname( f_path ) )
     zip_file.extract( f, f_path ) unless File.exist?( f_path )
   end
  end
end