code-snippets 0.2.13 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/code-snippets.rb +66 -46
  2. metadata +13 -4
data/lib/code-snippets.rb CHANGED
@@ -49,12 +49,12 @@ class CodeSnippets
49
49
 
50
50
  def show(id, current_user='guest')
51
51
 
52
- doc = Document.new('<result><summary/><records/></result>')
52
+ doc = Rexle.new('<result><summary/><records/></result>')
53
53
  entry = @blog.entry(id).dup
54
- doc.root.elements['records'].add entry
54
+ doc.root.element('records').add entry
55
55
 
56
56
  (tags = " [%s]" % entry.text('tags').split(/\s/).join('] [')) if entry.text('tags')
57
- doc.root.elements['summary'].add Element.new('title').add_text(entry.text('title').to_s + tags)
57
+ doc.root.element('summary').add Rexle::Element.new('title').add_text(entry.text('title').to_s + tags)
58
58
  prepare_doc doc
59
59
 
60
60
  render_html(doc, @xsl_doc_single, current_user).to_s
@@ -69,7 +69,7 @@ class CodeSnippets
69
69
  # fetch the user
70
70
  user = h[:user]
71
71
  h.delete :user
72
- @blog.update_user(user, id, h)
72
+ @blog.update_entry(user, id, h)
73
73
  end
74
74
 
75
75
  # -- xml interface ------------
@@ -122,15 +122,19 @@ class CodeSnippets
122
122
 
123
123
  def html_cached(context, summary, current_user='guest', &b)
124
124
  c = context.join
125
- @page_cache.read(c + current_user) do
125
+
126
+ out = @page_cache.read(c + current_user) do
126
127
  view_html(@doc_cache.read(c + current_user){b.call}, c, summary, current_user).to_s
127
128
  end
129
+
130
+ out
128
131
  end
129
132
 
130
133
  def view_html(raw_doc, context, summary, current_user='guest')
131
-
134
+
132
135
  blk = lambda do
133
- page_doc = Document.new(raw_doc.to_s)
136
+
137
+ page_doc = Rexle.new(raw_doc.to_s)
134
138
 
135
139
  prepare_doc(page_doc)
136
140
  total_records, total_pages, page_number = %w(total_records total_pages page_number)\
@@ -147,24 +151,31 @@ class CodeSnippets
147
151
  summary[:rss_url] = (@args.unshift '/rss').join('/')
148
152
 
149
153
  summary.each do |name, text|
150
- page_doc.root.elements['summary'].add Element.new(name.to_s).add_text(text.to_s)
154
+ page_doc.root.element('summary').add Rexle::Element.new(name.to_s).add_text(text.to_s)
151
155
  end
152
156
 
153
- summary_node = XPath.first(page_doc.root, 'summary')
154
- tags = Document.new(@tags.to_s).root.elements['records']
155
- tags.name = 'tags'
156
- summary_node.add tags
157
- latest_posts = Document.new(@latest_posts.to_s).root.elements['records']
158
- latest_posts.name = 'latest_posts'
159
- summary_node.add latest_posts
157
+ summary_node = page_doc.element('summary')
158
+
159
+ if @tags and @tags.length >= 1 then
160
+ tags = Rexle.new(@tags.to_s).root.element('records')
161
+ tags.name = 'tags'
162
+ summary_node.add_element tags
163
+ end
164
+
165
+ if @latest_posts then
166
+ latest_posts = Rexle.new(@latest_posts.to_s).root.element('records')
167
+ latest_posts.name = 'latest_posts'
168
+ summary_node.add latest_posts
169
+ end
170
+
160
171
  bottom_summary = summary_node.dup
161
172
  bottom_summary.name = 'bottom_summary'
162
173
  page_doc.root.add bottom_summary
163
- page_doc
174
+ page_doc.xml
164
175
  end
165
-
176
+
166
177
  xml_doc = context != '1' ? @hc_xml.read(context+current_user, &blk) : blk.call
167
-
178
+
168
179
  render_html(xml_doc, @xsl_doc, current_user)
169
180
 
170
181
  end
@@ -174,43 +185,49 @@ class CodeSnippets
174
185
  page_doc.root.name = 'snippets'
175
186
  convertor = Syntax::Convertors::HTML.for_syntax "ruby"
176
187
 
177
- XPath.each(page_doc.root,'records/entry') do |entry|
188
+ page_doc.xpath('records/entry').each do |entry|
178
189
 
179
- line = entry.elements['body/text()'].to_s.gsub('&amp;','&#38;')
190
+ line = entry.element('body/text()').gsub('&amp;','&#38;')
180
191
  buffer = line.gsub(/&lt;(\/?)(code|pre|span|br|a\s|\/a)(\/?[^&]+)?&gt;/,'<\1\2\3>').gsub('&#38;','&amp;')
181
192
 
182
- doc_body = Document.new("<div class='post-body'>%s</div>" % buffer)
193
+ doc_body = Rexle.new("<div class='post-body'>%s</div>" % buffer)
183
194
 
184
- XPath.each(doc_body.root, 'code') do |code|
185
- styled_code = code.text.to_s[/<span>/] ? code.text.to_s : convertor.convert(REXML::Text::unnormalize(code.text.to_s))
186
- code.add Document.new("%s" % styled_code).root
195
+ doc_body.xpath('code').each do |code|
196
+ styled_code = code.text[/<span>/] ? code.text : convertor.convert(code.text.unescape)
197
+ code.add Rexle.new("%s" % styled_code).root
187
198
  code.text = ''
188
199
  code.name = '_code'
189
200
  end
190
201
 
191
- body = entry.elements['body']
192
- body.parent.delete body
193
- entry.add Document.new(doc_body.to_s.gsub(/<\/?_code>/,'')).root
202
+ entry.delete('body')
203
+
204
+ entry.add Rexle.new(doc_body.to_s.gsub(/<\/?_code>/,'')).root
194
205
 
195
- tags = entry.elements['tags']
196
- tags.text.to_s.split(/\s/).map {|x| tags.add Element.new('tag').add_text(x)}
206
+ tags = entry.element('tags')
207
+ tags.text.to_s.split(/\s/).map {|x| tags.add Rexle::Element.new('tag').add_text(x)}
197
208
  tags.text = ''
198
209
 
199
- entry.add_attribute('created',Time.parse(entry.attribute('created').value).strftime("%b %d, %Y"))
210
+ entry.add_attribute(created: Time.parse(entry.attributes[:created]).strftime("%b %d, %Y"))
200
211
 
201
212
  end
202
213
 
203
214
  end
204
215
 
205
216
  def load()
206
- @blog = DynarexUsersBlog.new(@h[:filepath], @h[:default_user])
207
- @tags = sidetags(@blog.tags)
208
- @latest_posts = latest_posts(@blog)
217
+
218
+ @blog = DynarexUsersBlog.new(filepath: @h[:filepath], user: @h[:default_user])
219
+
220
+ if @blog.tags.length > 0 then
221
+ @tags = sidetags(@blog.tags)
222
+ @latest_posts = latest_posts(@blog)
223
+ end
224
+
209
225
  @page_cache = HashCache.new
210
226
  @doc_cache = HashCache.new
211
227
  @hc_xml = HashCache.new(file_cache: true)
212
228
  @rss_cache = HashCache.new
213
229
  @args = []
230
+
214
231
  load_renderer()
215
232
  end
216
233
 
@@ -225,7 +242,7 @@ class CodeSnippets
225
242
  weight = 100 / (biggest_tag / word_size.to_i)
226
243
  gauge = (weight / 10).to_i
227
244
  gauge = 8 if gauge > 8
228
- dynarex.create name: word, gauge: gauge, count: word_size
245
+ dynarex.create name: word, gauge: gauge.to_s, count: word_size
229
246
  end
230
247
 
231
248
  dynarex.to_xml
@@ -236,8 +253,8 @@ class CodeSnippets
236
253
 
237
254
  %w(ruby javascript html xml array shell linux bash).each do |x|
238
255
  posts.create.posts(title: x) do |create|
239
- XPath.each(blog.tag(x).page(1).root, "records/entry[position() < 4]") do |entry|
240
- create.entry title: entry.text('title').to_s, id: entry.attribute('id').value.to_s
256
+ blog.tag(x).page(1).xpath("records/entry[position() < 4]").each do |entry|
257
+ create.entry title: entry.text('title').to_s, id: entry.attribute(:id)
241
258
  end
242
259
  end
243
260
  end
@@ -257,14 +274,15 @@ class CodeSnippets
257
274
  @xsl_doc = Document.new(main.to_s)
258
275
  XPath.each(snippets.root, 'xsl:template') {|template| @xsl_doc.root.add template }
259
276
 
277
+
260
278
  @xsl_doc_single = Document.new(main.to_s)
261
279
  XPath.each(snippets_entry.root, 'xsl:template') {|template| @xsl_doc_single.root.add template }
262
280
 
263
281
  end
264
282
 
265
- def render_html(xml_doc, xsl_doc, current_user='guest')
266
- xsl_params = ['current_user', current_user]
267
- Nokogiri::XSLT(xsl_doc.to_s).transform(Nokogiri::XML(xml_doc.to_s), Nokogiri::XSLT.quote_params(xsl_params))
283
+ def render_html(xml, xsl_doc, current_user='guest')
284
+ xsl_params = ['current_user', current_user]
285
+ Nokogiri::XSLT(xsl_doc.to_s).transform(Nokogiri::XML(xml.to_s), Nokogiri::XSLT.quote_params(xsl_params))
268
286
  end
269
287
 
270
288
  alias render_rss render_html
@@ -276,13 +294,15 @@ class CodeSnippets
276
294
  end
277
295
 
278
296
  def view_rss(doc)
279
- page_doc = Document.new(doc.to_s)
280
- rss_doc = Document.new(render_rss(page_doc,@xsl_rssdoc).to_s)
281
-
282
- XPath.each(rss_doc.root,'channel/item/description') do |desc|
283
- desc.text = desc.text.to_s.gsub(/\n/,'\0<br />')
297
+
298
+ page_doc = Rexle.new(doc.xml)
299
+ rss_doc = Rexle.new(render_rss(page_doc,@xsl_rssdoc).to_s)
300
+
301
+ rss_doc.xpath('channel/item/description').each do |desc|
302
+ desc.text = desc.text.gsub(/\n/,'\0<br />')
284
303
  end
285
- rss_doc.to_s
304
+
305
+ rss_doc.xml
286
306
  end
287
307
 
288
308
  def cache_reset()
@@ -291,4 +311,4 @@ class CodeSnippets
291
311
  @hc_xml.reset
292
312
  end
293
313
 
294
- end
314
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code-snippets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.13
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors: []
7
7
 
@@ -9,10 +9,19 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-10-04 00:00:00 +01:00
12
+ date: 2011-02-14 00:00:00 +00:00
13
13
  default_executable:
14
- dependencies: []
15
-
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: dynarex-usersblog
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
16
25
  description:
17
26
  email:
18
27
  executables: []