Last active
October 2, 2021 10:31
-
-
Save a6b8/e9496ea255d8db40ade0cd7ac4217cee to your computer and use it in GitHub Desktop.
Overview Markdown Table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def helper( obj, index ) | |
| puts obj[:projects][ index ] | |
| current = obj[:projects][ index ] | |
| puts '> INPUT' | |
| current.each do | k, v | | |
| space = ( 15 - k.length ).times.map { | a | ' ' }.join( '' ) | |
| puts( " #{k}:#{space}#{v}" ) | |
| end | |
| puts | |
| item = md_url( current, index, obj ) | |
| puts '> TO URL' | |
| item.each do | k, v | | |
| space = ( 15 - k.length ).times.map { | a | ' ' }.join( '' ) | |
| puts( " #{k}:#{space}#{v}" ) | |
| end | |
| puts | |
| puts '> TO HTML' | |
| html = md_html( current, item, obj ) | |
| html.each do | k, v | | |
| space = ( 15 - k.length ).times.map { | a | ' ' }.join( '' ) | |
| puts( " #{k}:#{space}#{v.to_s}" ) | |
| end | |
| return true | |
| end | |
| def markdown_url( item, index, obj ) | |
| def theme( index, obj, add={} ) | |
| a = obj[:theme] | |
| .map { | k, v | [ k, k.eql?( :color ) ? v[ index % 2 ] : v ] } | |
| .inject( {} ) { | item, k | item[ k[ 0 ] ] = k[ 1 ]; item } | |
| b = URI.encode_www_form( a.merge( add ) ) | |
| result = "?#{b}" | |
| return result | |
| end | |
| def title( item, obj ) | |
| obj[:endpoints][:github_repo] | |
| .gsub( '{{gh_user}}', obj[:meta][:github_user] ) | |
| .gsub( '{{gh_repo}}', item[:github] ) | |
| end | |
| def doc( item, obj ) | |
| if item[:docs] | |
| obj[:endpoints][:github_repo] | |
| .gsub( '{{gh_user}}', obj[:meta][:github_user] ) | |
| .gsub( '{{gh_repo}}', item[:github] ) | |
| .concat( '#table-of-contents' ) | |
| end | |
| end | |
| def release( item, index, obj ) | |
| result = nil | |
| case item[:type] | |
| when :ruby | |
| if item.keys.include? :gem | |
| result = obj[:endpoints][:shield_io] | |
| .gsub( '{{label}}', 'RubyGem' ) | |
| .gsub( '{{color}}', obj[:theme][:color][ index%2 ]) | |
| end | |
| when :javascript | |
| result = obj[:endpoints][:shield_io] | |
| .gsub( '{{label}}', 'cdnjs' ) | |
| .gsub( '{{color}}', obj[:theme][:color][ index%2 ] ) | |
| when :php | |
| else | |
| puts "#{item[:type]} not set." | |
| end | |
| return result | |
| end | |
| def version( item, index, obj ) | |
| result = nil | |
| case item[:type] | |
| when :ruby | |
| if item.keys.include? :gem | |
| result = obj[:endpoints][:gem_version] | |
| .gsub( '{{gem_name}}', item[:gem] ) | |
| end | |
| when :javascript | |
| if item.keys.include? :js | |
| result = obj[:endpoints][:cdnjs] | |
| .gsub( '{{js_name}}', item[:js] ) | |
| end | |
| when :php | |
| else | |
| puts "#{item[:type]} not set." | |
| end | |
| !result.nil? ? result.concat( theme( index, obj, { label: '' } ) ) : '' | |
| return result | |
| end | |
| def stats( item, index, obj ) | |
| result = nil | |
| case item[:type] | |
| when :ruby | |
| if item.keys.include? :gem | |
| result = obj[:endpoints][:gem_downloads] | |
| .gsub( '{{gem_name}}', item[:gem] ) | |
| end | |
| when :javascript | |
| when :php | |
| else | |
| puts "#{item[:type]} not set." | |
| end | |
| !result.nil? ? result.concat( theme( index, obj, { label: '' } ) ) : '' | |
| return result | |
| end | |
| def updated( item, index, obj ) | |
| result = obj[:endpoints][:github_last_commit] | |
| .gsub( '{{gh_user}}', obj[:meta][:github_user] ) | |
| .gsub( '{{gh_repo}}', item[:github] ) | |
| !result.nil? ? result.concat( theme( index, obj, { label: '' } ) ) : '' | |
| end | |
| def test( item, index, obj ) | |
| result = nil | |
| if item.keys.include? :circle_ci | |
| result = obj[:endpoints][:circle_ci] | |
| .gsub( '{{gh_user}}', obj[:meta][:github_user] ) | |
| .gsub( '{{gh_repo}}', item[:github] ) | |
| result.concat( theme( index, obj, { token: item[:circle_ci], label: '' } ) ) | |
| end | |
| return result | |
| end | |
| struct = { | |
| title: nil, | |
| doc: nil, | |
| release: nil, | |
| version: nil, | |
| stats: nil, | |
| updated: nil, | |
| test: nil | |
| } | |
| struct[:title] = title( item, obj ) | |
| struct[:doc] = doc( item, obj ) | |
| struct[:release] = release( item, index, obj ) | |
| struct[:version] = version( item, index, obj ) | |
| struct[:stats] = stats( item, index, obj ) | |
| struct[:updated] = updated( item, index, obj ) | |
| struct[:test] = test( item, index, obj ) | |
| return struct | |
| end | |
| def markdown_html( current, item, obj ) | |
| def image_md( name, url ) | |
| "[#{name}](#{url})" | |
| end | |
| def image( str ) | |
| str | |
| .insert( 0, '<img src="') | |
| .concat( '">') | |
| end | |
| def a( str, href="#latest-projects" ) | |
| str | |
| .insert( 0, "<a href=\"#{href}\">" ) | |
| .concat( '</a>') | |
| end | |
| [ :title, :doc, :release, :version, :stats, :updated, :test ].each do | key | | |
| str = nil | |
| if !item[ key ].nil? | |
| case key | |
| when :title | |
| str = image_md( current[:name], item[ key ] ) | |
| .insert( 0, obj[:types][ current[:type] ] + ' ' ) | |
| when :doc | |
| str = image_md( obj[:types][:docs], item[ key ] ) | |
| when :release | |
| if !item[ key ].nil? | |
| str = image( item[ key ] ) | |
| case current[:type] | |
| when :ruby | |
| s = obj[:endpoints][:ruby_gem] | |
| .gsub( '{{gem_name}}', current[:gem] ) | |
| str = a( str, s ) | |
| when :javascript | |
| s = obj[:endpoints][:cdnjs_profile] | |
| .gsub( '{{js_name}}', current[:js] ) | |
| str = a( str, s ) | |
| else | |
| str = a( str ) | |
| end | |
| end | |
| when :version | |
| str = image( item[ key ] ) | |
| str = a( str ) | |
| when :stats | |
| str = image( item[ key ] ) | |
| str = a( str ) | |
| when :updated | |
| str = image( item[ key ] ) | |
| url = obj[:endpoints][:github_api_repo] | |
| .gsub( '{{gh_user}}', obj[:meta][:github_user] ) | |
| .gsub( '{{gh_repo}}', current[:github] ) | |
| str = a( str, url ) | |
| when :test | |
| if !item[ key ].nil? | |
| str = image( item[ key ] ) | |
| url = obj[:endpoints][:circle_ci_status] | |
| .gsub( '{{gh_user}}', obj[:meta][:github_user] ) | |
| .gsub( '{{gh_repo}}', current[:github] ) | |
| .gsub( '{{cc_token}}', current[:circle_ci] ) | |
| str = a( str, url ) | |
| end | |
| else | |
| end | |
| end | |
| !str.nil? ? item[ key ] = str : item[ key ] = ' ' | |
| end | |
| return item | |
| end | |
| def markdown_table( obj ) | |
| as = obj[:projects].map.with_index do | project, index | | |
| item = md_url( project, index, obj ) | |
| html = md_html( project, item, obj ) | |
| end | |
| strs = [] | |
| .push( as[ 0 ].map { | k, v | k.capitalize.to_s }.join( ' | ' ) ) | |
| .push( as[ 0 ].keys.length.times.map { | a | ':--' }.join(' | ') ) | |
| .push( as.map { | a | a.map { | k, v | v }.join( ' | ' ) } ) | |
| .flatten | |
| .map { | row | row.insert( 0, '| ').concat(' |' ) } | |
| .join( "\n" ) | |
| return strs | |
| end | |
| helper( hash, 1 ) | |
| table = markdown_table( hash ) | |
| #File.open( 'test.md', "w" ) { | f | f.write( markdown ) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment