code-snippets 0.1.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 +240 -0
  2. metadata +55 -0
@@ -0,0 +1,240 @@
1
+ #!/usr/bin/ruby
2
+
3
+ # file:code-snippets.rb
4
+
5
+ require 'dynarex-usersblog'
6
+ require 'nokogiri'
7
+ require 'time'
8
+ require 'syntax/convertors/html'
9
+
10
+ class CodeSnippets
11
+ include REXML
12
+
13
+ def initialize(opt={})
14
+ @h = opt[:config]
15
+ load()
16
+ end
17
+
18
+ def page(n='1')
19
+ html_page n
20
+ end
21
+
22
+ def tag(tag, n='1')
23
+ html_tag tag, n
24
+ end
25
+
26
+ def user(user, n='1')
27
+ html_user(user, n)
28
+ end
29
+
30
+ def user_tag(user, tag, n='1')
31
+ html_user_tag(user, tag, n)
32
+ end
33
+
34
+ def rss()
35
+ rss_cached('1') { @blog.page(1) }
36
+ end
37
+
38
+ def rss_tag(tag)
39
+ rss_cached(tag) { @blog.tag(tag).page(1) }
40
+ end
41
+
42
+ def rss_user(user)
43
+ rss_cached(user) { @blog.user(user).page(1) }
44
+ end
45
+
46
+ def rss_user_tag(user, tag)
47
+ rss_cached(user+tag) { @blog.user(user).tag(tag).page(1) }
48
+ end
49
+
50
+ #private
51
+
52
+ def html_page(n)
53
+ @args = [n]
54
+ summary = {title: 'Snippets'}
55
+ html_cached(context=@args, summary) { @blog.page(n.to_i) }
56
+ end
57
+
58
+ def html_tag(tag, pg_n)
59
+ @args = [tag, pg_n]
60
+ summary = {title: tag + ' code', tag: tag}
61
+ html_cached(context=@args, summary) { @blog.tag(tag).page(pg_n.to_i) }
62
+ end
63
+
64
+ def html_user(user, pg_n)
65
+ @args = [user, pg_n]
66
+ summary = {title: 'Snippets', user: user}
67
+ html_cached(context=@args, summary) { @blog.user(user).page(pg_n.to_i) }
68
+ end
69
+
70
+ def html_user_tag(user, tag, pg_n)
71
+ @args = [user, tag, pg_n]
72
+ summary = {title: 'Snippets', user: user, tag: tag}
73
+ html_cached(context=@args, summary) { @blog.user(user).tag(tag).page(pg_n.to_i) }
74
+ end
75
+
76
+ def html_cached(context, summary, &b)
77
+ c = context.join
78
+ @page_cache.read(c) do
79
+ view_html(@doc_cache.read(c){b.call}, c, summary)
80
+ end
81
+ end
82
+
83
+ def view_html(raw_doc, context, summary)
84
+
85
+ blk = lambda do
86
+ page_doc = Document.new(raw_doc.to_s)
87
+
88
+ prepare_doc(page_doc)
89
+ total_records, total_pages, page_number = %w(total_records total_pages page_number)\
90
+ .map {|x| page_doc.root.text('summary/' + x).to_s.to_i}
91
+
92
+ k = page_number * 10
93
+ pages = "%d-%d" % [k-9, total_records >= k ? k : total_records]
94
+
95
+ summary[:pages] = pages
96
+ summary[:prev_page] = page_number - 1 if page_number > 1
97
+ summary[:next_page] = page_number + 1 if page_number < total_pages
98
+
99
+ @args.pop if @args.length > 0 and @args[-1][/^\d+$/]
100
+ summary[:rss_url] = (@args.unshift '/rss').join('/')
101
+
102
+ summary.each do |name, text|
103
+ page_doc.root.elements['summary'].add Element.new(name.to_s).add_text(text.to_s)
104
+ end
105
+
106
+ summary_node = XPath.first(page_doc.root, 'summary')
107
+ tags = Document.new(@tags.to_s).root.elements['records']
108
+ tags.name = 'tags'
109
+ summary_node.add tags
110
+ latest_posts = Document.new(@latest_posts.to_s).root.elements['records']
111
+ latest_posts.name = 'latest_posts'
112
+ summary_node.add latest_posts
113
+ bottom_summary = summary_node.dup
114
+ bottom_summary.name = 'bottom_summary'
115
+ page_doc.root.add bottom_summary
116
+ page_doc
117
+ end
118
+
119
+ xml_doc = context != '1' ? @hc_xml.read(context, &blk) : blk.call
120
+
121
+ render_html(xml_doc, @xsl_doc)
122
+
123
+ end
124
+
125
+
126
+ def prepare_doc(page_doc)
127
+ page_doc.root.name = 'snippets'
128
+ convertor = Syntax::Convertors::HTML.for_syntax "ruby"
129
+
130
+ XPath.each(page_doc.root,'records/entry') do |entry|
131
+
132
+ line = entry.elements['body/text()'].to_s.gsub('&amp;','&#38;')
133
+ buffer = line.gsub(/&lt;(\/?)(code|pre|span|br|a\s|\/a)(\/?[^&]+)?&gt;/,'<\1\2\3>').gsub('&#38;','&amp;')
134
+
135
+ doc_body = Document.new("<div class='post-body'>%s</div>" % buffer)
136
+
137
+ XPath.each(doc_body.root, 'code') do |code|
138
+ styled_code = code.text.to_s[/<span>/] ? code.text.to_s : convertor.convert(REXML::Text::unnormalize(code.text.to_s))
139
+ code.add Document.new("%s" % styled_code).root
140
+ code.text = ''
141
+ code.name = '_code'
142
+ end
143
+
144
+ body = entry.elements['body']
145
+ body.parent.delete body
146
+ entry.add Document.new(doc_body.to_s.gsub(/<\/?_code>/,'')).root
147
+
148
+ tags = entry.elements['tags']
149
+ tags.text.to_s.split(/\s/).map {|x| tags.add Element.new('tag').add_text(x)}
150
+ tags.text = ''
151
+
152
+ entry.add_attribute('created',Time.parse(entry.attribute('created').value).strftime("%b %d, %Y"))
153
+
154
+ end
155
+
156
+ end
157
+
158
+ def load()
159
+ @blog = DynarexUsersBlog.new(@h[:filepath], @h[:default_user])
160
+ @tags = sidetags(@blog.tags)
161
+ @latest_posts = latest_posts(@blog)
162
+ @page_cache = HashCache.new
163
+ @doc_cache = HashCache.new
164
+ @hc_xml = HashCache.new(file_cache: true)
165
+ @rss_cache = HashCache.new
166
+ @args = []
167
+ load_renderer()
168
+ end
169
+
170
+ def sidetags(all_tags)
171
+
172
+ tags = all_tags.sort_by {|name,count| -count.to_i}[0..49]
173
+ biggest_tag = tags.first[1].to_i
174
+ abc_tags = tags.sort_by &:first
175
+
176
+ dynarex = Dynarex.new('tags/tag(name,gauge,count)')
177
+ abc_tags.each do |word, word_size|
178
+ weight = 100 / (biggest_tag / word_size.to_i)
179
+ gauge = (weight / 10).to_i
180
+ gauge = 8 if gauge > 8
181
+ dynarex.create name: word, gauge: gauge, count: word_size
182
+ end
183
+
184
+ dynarex.to_xml
185
+ end
186
+
187
+ def latest_posts(blog)
188
+ posts = Polyrex.new('latest_posts/posts[title]/entry[title,id]')
189
+
190
+ %w(ruby javascript html xml array shell linux bash).each do |x|
191
+ posts.create.posts(title: x) do |create|
192
+ XPath.each(blog.tag(x).page(1).root, "records/entry[position() < 4]") do |entry|
193
+ create.entry title: entry.text('title').to_s, id: entry.attribute('id').value.to_s
194
+ end
195
+ end
196
+ end
197
+
198
+ posts.to_xml
199
+ end
200
+
201
+ def load_renderer()
202
+
203
+ xsl = @h[:xsl]
204
+ urls = [xsl[:outer], xsl[:page], xsl[:entry], xsl[:rss]]
205
+
206
+ main, snippets, snippets_entry, @xsl_rssdoc = urls.map do |url|
207
+ Document.new(open(url, "UserAgent" => "Sinatra-Rscript").read)
208
+ end
209
+
210
+ @xsl_doc = Document.new(main.to_s)
211
+ XPath.each(snippets.root, 'xsl:template') {|template| @xsl_doc.root.add template }
212
+
213
+ @xsl_doc_single = Document.new(main.to_s)
214
+ XPath.each(snippets_entry.root, 'xsl:template') {|template| @xsl_doc_single.root.add template }
215
+
216
+ end
217
+
218
+ def render_html(xml_doc, xsl_doc)
219
+ Nokogiri::XSLT(xsl_doc.to_s).transform(Nokogiri::XML(xml_doc.to_s))
220
+ end
221
+
222
+ alias render_rss render_html
223
+
224
+ def rss_cached(context, &b)
225
+ @rss_cache.read(context) do
226
+ view_rss(@doc_cache.read(context){b.call})
227
+ end
228
+ end
229
+
230
+ def view_rss(doc)
231
+ page_doc = Document.new(doc.to_s)
232
+ rss_doc = Document.new(render_rss(page_doc,@xsl_rssdoc).to_s)
233
+
234
+ XPath.each(rss_doc.root,'channel/item/description') do |desc|
235
+ desc.text = desc.text.to_s.gsub(/\n/,'\0<br />')
236
+ end
237
+ rss_doc.to_s
238
+ end
239
+
240
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: code-snippets
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors: []
7
+
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-08-05 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email:
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - lib/code-snippets.rb
26
+ has_rdoc: true
27
+ homepage:
28
+ licenses: []
29
+
30
+ post_install_message:
31
+ rdoc_options: []
32
+
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: "0"
40
+ version:
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: "0"
46
+ version:
47
+ requirements: []
48
+
49
+ rubyforge_project:
50
+ rubygems_version: 1.3.5
51
+ signing_key:
52
+ specification_version: 3
53
+ summary: code-snippets
54
+ test_files: []
55
+