Module: Oreilly::Snippets

Defined in:
lib/oreilly/snippets.rb,
lib/oreilly/snippets/version.rb

Constant Summary collapse

VERSION =
"0.0.18"
@@_config =
{}

Class Method Summary collapse

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



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/oreilly/snippets.rb', line 161

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
85
86
87
88
89
90
91
92
93
94
95
96
# 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_prefix = s[:callouts_prefix]
  callouts_wrap = s[:callouts_wrap]
  callouts = s[:callouts]

  if callouts_wrap
    if callouts_prefix
      raise "Cannot use both callout prefix and wrap together"
    end

    if -1 == callouts_wrap.index( '{x}' )
      raise "Improper use of callouts: needs to be like '<!-- {x} -->' ('{x}' must be a placeholder)" 
    end
  end

  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, callouts_prefix, callouts_wrap )
  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



147
148
149
150
151
152
# File 'lib/oreilly/snippets.rb', line 147

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



201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/oreilly/snippets.rb', line 201

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



189
190
191
192
193
194
195
196
197
198
199
# File 'lib/oreilly/snippets.rb', line 189

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, comment_character = nil, wrap = nil) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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
# File 'lib/oreilly/snippets.rb', line 98

def self.process_callouts( input, callouts, comment_character = nil, wrap = nil )
  rv = nil
  # Strip them out and figure out the comment character
  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

      if lines.length > index and index >= 0
        callout = nil
        if wrap
          callout = wrap.gsub( '{x}', "<#{i+1}>" )
        else
          callout = "#{comment_character} <#{i+1}>" 
        end

        lines[index] += callout
      end
    end
    rv = lines.join "\n"
  end

  if ENV['OREILLY_SNIPPETS_DEBUG_LONG_LINES']
    rv.split( /\n/ ).each_with_index do |l,i|
      if l.length > 70
        raise "Line #{i+1} is too long: #{l.length} characters long"
      end
    end
  end

  rv
end

.scrub_other_identifiers(s, comments) ⇒ Object



182
183
184
185
186
187
# File 'lib/oreilly/snippets.rb', line 182

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



154
155
156
157
158
159
# File 'lib/oreilly/snippets.rb', line 154

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