Module: Oreilly::Snippets
- Defined in:
- lib/oreilly/snippets.rb,
lib/oreilly/snippets/version.rb
Constant Summary collapse
- VERSION =
"0.0.15"
- @@_config =
{}
Class Method Summary collapse
- .config(opts) ⇒ Object
- .flatten_it(content) ⇒ Object
- .get_content_from_file(s) ⇒ Object
- .normalize_callouts(rv) ⇒ Object
- .parse(input) ⇒ Object
- .process(input) ⇒ Object
- .process_callouts(input, callouts) ⇒ Object
- .scrub_other_identifiers(s, comments) ⇒ Object
- .skip_flattening(language) ⇒ Object
Class Method Details
.config(opts) ⇒ Object
14 15 16 |
# File 'lib/oreilly/snippets.rb', line 14 def self.config( opts ) @@_config.merge!( opts ) end |
.flatten_it(content) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/oreilly/snippets.rb', line 131 def self.flatten_it( content ) # find the smallest indent level, and then strip that off the beginning of all lines smallest = nil lines = content.split "\n" lines.each do |l| if l =~ /^(\s*)\S/ if smallest if $1.length < smallest.length smallest = $1 end else smallest = $1 end end end content.gsub!( /^#{smallest}/, '' ) if smallest content end |
.get_content_from_file(s) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/oreilly/snippets.rb', line 18 def self.get_content_from_file( s ) spec = s[:filename] identifier = s[:identifier] language = s[:language] sha = s[:sha] numbers = s[:lines] flatten = s[:flatten] normcallouts = s[:normcallouts] callouts = s[:callouts] contents = nil line_numbers = nil error = false if numbers sae = numbers.split( ".." ).map { |d| Integer(d)-1 } line_numbers = [sae[0], sae[1]] end if sha if sha[0..2].eql? "xxx" contents = "PLACEHOLDER TEXT, UPDATE WITH CORRECT SHA HASH" else # Use the filename to change into the directory and use git-show spec = "." unless spec Dir.chdir spec do contents = `git show #{sha}` error = true unless contents end end else contents = File.read( spec ) end # If line numbers are there, provide only that content if line_numbers contents = contents.split( /\n/ )[line_numbers[0]..line_numbers[1]].join( "\n" ) end rv = nil if identifier comments = COMMENTS[language.to_sym] re = /#{comments} BEGIN #{identifier}\n(.*)\n#{comments} END #{identifier}\n/m m = contents.match( re ) rv = m[1] else rv = contents end # puts "Should we skip flattening? #{language}" unless skip_flattening( language ) if flatten or @@_config[:flatten] rv = flatten_it( rv ) end end if callouts rv = process_callouts( rv, callouts ) elsif normcallouts rv = normalize_callouts( rv ) end rv = "INVALID SNIPPET, WARNING" if error # rv = scrub_other_identifiers( contents, comments ) rv end |
.normalize_callouts(rv) ⇒ Object
117 118 119 120 121 122 |
# File 'lib/oreilly/snippets.rb', line 117 def self.normalize_callouts( rv ) # Find something that looks like comment character + whitespace + < + number + > index = 0 rv.gsub!( /([\/#]) <\d*>/ ) { |c| index += 1; "#{$1} <#{index}>" } rv end |
.parse(input) ⇒ Object
171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/oreilly/snippets.rb', line 171 def self.parse( input ) output = [] input.scan( /(\[[^\]]*\])(\s+)(snippet[^\s]*)(.*?)\3/m ) do |m| # Add it all up, and include the snippet piece (second to last captured) full = m.join( "" ) + m[m.length-2] match = {} m[0].scan( /([^=\[,\s]*)="([^"]*)"/ ) do |kv| match[kv[0].to_sym] = kv[1] end match[:full] = full output << match end output end |
.process(input) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/oreilly/snippets.rb', line 159 def self.process( input ) snippets = parse( input ) rv = input if snippets and snippets.length > 0 snippets.each do |s| content = get_content_from_file( s ) # s[:filename], s[:identifier], s[:language], s[:sha], s[:lines], s[:flatten], s[:normcallouts] ) rv = rv.gsub( s[:full], content ) end end rv end |
.process_callouts(input, callouts) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/oreilly/snippets.rb', line 86 def self.process_callouts( input, callouts ) rv = nil # Strip them out and figure out the comment character comment_character = nil rv = input.gsub( /([#\/]\/?) ?<\d+>/ ) { |c| comment_character = $1; '' } unless comment_character # OK, we need to scan for it and hope to figure it out if rv =~ /\/\/[\/]+$/ comment_character = '/' elsif rv =~ /#[^#]+$/ comment_character = '#' end end unless comment_character comment_character = '//' end unless callouts.eql? "" list = callouts.split /,/ lines = rv.split /\n/ list.each_with_index do |c,i| index = c.to_i - 1 lines[index] += "#{comment_character} <#{i+1}>" if lines.length > index and index >= 0 end rv = lines.join "\n" end rv end |
.scrub_other_identifiers(s, comments) ⇒ Object
152 153 154 155 156 157 |
# File 'lib/oreilly/snippets.rb', line 152 def self.scrub_other_identifiers( s, comments ) # puts s re = /#{comments} BEGIN \S+\n(.*)\n#{comments} END \S+\n/m s.gsub!( re, $1 ) s end |
.skip_flattening(language) ⇒ Object
124 125 126 127 128 129 |
# File 'lib/oreilly/snippets.rb', line 124 def self.skip_flattening( language ) rv = ( !!@@_config[:skip_flattening] and language and !!@@_config[:skip_flattening][language.to_sym] ) # rv = ( !!@@_config[:skip_flattening] and !!@@_config[:skip_flattening][language.to_sym] ) # puts "Skipping flattening for #{language} / #{@@_config[:skip_flattening][language.to_sym]} / #{rv} / (#{@@_config[:skip_flattening].inspect})" if rv rv end |