dynarex-blog 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/dynarex-blog.rb +211 -0
- metadata +74 -0
data/lib/dynarex-blog.rb
ADDED
@@ -0,0 +1,211 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
# file: dynarex-blog.rb
|
4
|
+
|
5
|
+
require 'rexml/document'
|
6
|
+
require 'polyrex'
|
7
|
+
require 'dynarex'
|
8
|
+
|
9
|
+
class DynarexBlog
|
10
|
+
include REXML
|
11
|
+
|
12
|
+
def initialize(file_path='./')
|
13
|
+
@file_path = file_path[/\/$/] ? file_path : file_path + '/'
|
14
|
+
if File.exists? (@file_path + 'index.xml') then open() else fresh_start() end
|
15
|
+
@current_lookup = 'entry_lookup.xml'
|
16
|
+
end
|
17
|
+
|
18
|
+
def create_entry(record={})
|
19
|
+
|
20
|
+
@id += 1
|
21
|
+
# create a new record
|
22
|
+
@index.create record, @id
|
23
|
+
|
24
|
+
# if there is more than 10 records shift the 1st record.
|
25
|
+
if @index.records.length > 10 then
|
26
|
+
# '+++ deleting the 1st index record +++'
|
27
|
+
@index.delete @index.records.to_a[0][-1][:id]
|
28
|
+
end
|
29
|
+
@index.save @file_path + 'index.xml'
|
30
|
+
|
31
|
+
create_record(record, @id.to_s, name='entry', type='main')
|
32
|
+
record[:tags].split(/\s/).each do |tag|
|
33
|
+
create_record(record, @id.to_s, name=tag, type='tags')
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
def delete(id=0)
|
39
|
+
|
40
|
+
# delete from the tags files (entry and lookup), the entry file and lookup
|
41
|
+
# look the id up in lookup.xml
|
42
|
+
|
43
|
+
doc_entry, entry = open_lookup_record 'entry', id
|
44
|
+
puts entry.to_s
|
45
|
+
|
46
|
+
dynarex_file = @file_path + entry.text('file').to_s
|
47
|
+
dynarex = Document.new(File.open(dynarex_file,'r').read)
|
48
|
+
dynarex_entry = XPath.first(dynarex.root, "records/entry[@id='#{id}']")
|
49
|
+
tags = dynarex_entry.text('tags').split(/\s/)
|
50
|
+
|
51
|
+
dynarex_entry.parent.delete dynarex_entry
|
52
|
+
File.open(dynarex_file,'w'){|f| dynarex.write f}
|
53
|
+
|
54
|
+
entry.parent.delete entry
|
55
|
+
lookup = "%s%s" % [@file_path, 'entry_lookup.xml']
|
56
|
+
File.open(lookup,'w'){|f| doc_entry.write f}
|
57
|
+
|
58
|
+
tags.each do |tag|
|
59
|
+
# find the lookup
|
60
|
+
doc_tag, tag_entry = open_lookup_record tag, id
|
61
|
+
delete_entry(doc_tag, tag_entry, tag, id)
|
62
|
+
end
|
63
|
+
|
64
|
+
doc = Document.new File.open(@file_path + 'index.xml','r').read
|
65
|
+
node = XPath.first(doc.root, "records/entry[@id='#{id}']")
|
66
|
+
|
67
|
+
if node then
|
68
|
+
node_records = XPath.first(doc.root, 'records')
|
69
|
+
node_records.parent.delete node_records
|
70
|
+
records = Element.new 'records'
|
71
|
+
|
72
|
+
new_records = select_page('entry_lookup.xml', 1)
|
73
|
+
new_records.each {|record| records.add record}
|
74
|
+
doc.root.add records
|
75
|
+
|
76
|
+
File.open(@file_path + 'index.xml', 'w'){|f| doc.write f}
|
77
|
+
@index = Dynarex.new @file_path + 'index.xml'
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
def fresh_start()
|
83
|
+
|
84
|
+
@id = 1
|
85
|
+
@dynarex = new_blog_file 'entry1.xml'
|
86
|
+
@index = new_blog_file 'index.xml'
|
87
|
+
|
88
|
+
@entities = Polyrex.new('entities/section[name]/entity[name,count]')
|
89
|
+
@entities.create.section({name: 'main'}, id='main')
|
90
|
+
@entities.id('main').create.entity(name: 'entry', count: '1')
|
91
|
+
@entities.create.section({name: 'tags'}, id='tags')
|
92
|
+
@entities.save @file_path + 'entities.xml'
|
93
|
+
|
94
|
+
@lookup = new_lookup_file 'entry_lookup.xml'
|
95
|
+
end
|
96
|
+
|
97
|
+
def open(file_path='./')
|
98
|
+
|
99
|
+
@file_path = file_path
|
100
|
+
@index = Dynarex.new @file_path + 'index.xml'
|
101
|
+
@id = @index.records ? @index.records.to_a[-1][-1][:id].to_i : 0
|
102
|
+
|
103
|
+
@entities = Polyrex.new @file_path + 'entities.xml'
|
104
|
+
|
105
|
+
end
|
106
|
+
|
107
|
+
def page(number)
|
108
|
+
select_page(@current_lookup, number)
|
109
|
+
end
|
110
|
+
|
111
|
+
private
|
112
|
+
|
113
|
+
def delete_entry(doc, node, lookup_filename, id)
|
114
|
+
file = @file_path + node.text('file').to_s
|
115
|
+
Dynarex.new(file).delete(id).save file
|
116
|
+
|
117
|
+
node.parent.delete node
|
118
|
+
lookup = "%s%s" % [@file_path, lookup_filename]
|
119
|
+
File.open(lookup,'w'){|f| doc.write f}
|
120
|
+
end
|
121
|
+
|
122
|
+
def open_lookup_record(name, id)
|
123
|
+
lookup_path = "%s%s_lookup.xml" % [@file_path, name]
|
124
|
+
lookup = Document.new File.open(lookup_path,'r').read
|
125
|
+
[lookup, XPath.first(lookup.root, "records/entry[id='#{id}']")]
|
126
|
+
end
|
127
|
+
|
128
|
+
def select_page(lookup, number)
|
129
|
+
|
130
|
+
doc = Document.new File.open(@file_path + lookup,'r').read
|
131
|
+
x1 = (number - 1) * 10
|
132
|
+
x2 = x1 + 9
|
133
|
+
|
134
|
+
a = XPath.match(doc.root,'records/entry').reverse[x1..x2]
|
135
|
+
xpath_ids = "entry[%s]" % a.map{|x| x.attribute('id').value}.map{|x| "@id='%s'" % x}.join(' or ')
|
136
|
+
|
137
|
+
temp_doc = Document.new '<root/>'
|
138
|
+
a.map{|x| x.text('file').to_s}.uniq.each do |file|
|
139
|
+
doc = Document.new File.open(file,'r').read
|
140
|
+
XPath.each(doc.root,'records/entry') do |entry|
|
141
|
+
temp_doc.root.add entry
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
result = Document.new '<result><summary/><records/></result>'
|
146
|
+
records = XPath.first(result.root, 'records')
|
147
|
+
XPath.each(temp_doc.root, xpath_ids) do |record|
|
148
|
+
records.add record
|
149
|
+
end
|
150
|
+
|
151
|
+
result
|
152
|
+
end
|
153
|
+
|
154
|
+
def new_blog_file(filename)
|
155
|
+
dynarex = Dynarex.new('entries/entry(title,body,tags)')
|
156
|
+
dynarex.summary[:format_mask].gsub!(/\s/,'; ')
|
157
|
+
dynarex.save @file_path + filename
|
158
|
+
dynarex
|
159
|
+
end
|
160
|
+
|
161
|
+
def new_lookup_file(filename)
|
162
|
+
lookup = Dynarex.new('entries/entry(id,file,year,month,uri)')
|
163
|
+
lookup.save @file_path + filename
|
164
|
+
lookup
|
165
|
+
end
|
166
|
+
|
167
|
+
def create_record(record, id, name, type)
|
168
|
+
|
169
|
+
entry_count = @entities.xpath "records/section[summary/name='#{type}']/records/entity/summary[name='#{name}']/count"
|
170
|
+
lookup_path = "%s%s_lookup.xml" % [@file_path, name]
|
171
|
+
|
172
|
+
if entry_count.nil?
|
173
|
+
@entities.id('tags').create.entity(name: name, count: '1')
|
174
|
+
@entities.save @file_path + 'entities.xml'
|
175
|
+
@entities = Polyrex.new @file_path + 'entities.xml'
|
176
|
+
entry_count = @entities.xpath "records/section[summary/name='#{type}']/records/entity/summary[name='#{name}']/count"
|
177
|
+
|
178
|
+
dynarex = Dynarex.new('entries/entry(title,body,tags)')
|
179
|
+
dynarex.summary[:format_mask].gsub!(/\s/,'; ')
|
180
|
+
dynarex_path = @file_path + name + '1.xml'
|
181
|
+
dynarex.save dynarex_path
|
182
|
+
|
183
|
+
new_lookup_file lookup_path
|
184
|
+
|
185
|
+
end
|
186
|
+
|
187
|
+
entry_file = "%s%s.xml" % [name, entry_count.text]
|
188
|
+
dynarex_path = "%s%s" % [@file_path, entry_file]
|
189
|
+
dynarex = Dynarex.new dynarex_path
|
190
|
+
|
191
|
+
dynarex.create record, id
|
192
|
+
dynarex.save dynarex_path
|
193
|
+
|
194
|
+
# add the record to lookup
|
195
|
+
|
196
|
+
lookup = Dynarex.new lookup_path
|
197
|
+
lookup.create id: id, file: entry_file, year: Time.now.strftime("%Y"), month: Time.now.strftime("%m"), uri: record[:title].gsub(/\s/,'-')
|
198
|
+
lookup.save lookup_path
|
199
|
+
|
200
|
+
# if there is 15 items create a new entries file
|
201
|
+
if dynarex.records.length >= 15 then
|
202
|
+
|
203
|
+
entry_count.text = (entry_count.text.to_i + 1).to_s
|
204
|
+
@entities.save @file_path + 'entities.xml'
|
205
|
+
|
206
|
+
dynarex = new_blog_file "%s%s.xml" % [name, entry_count.text]
|
207
|
+
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dynarex-blog
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors: []
|
7
|
+
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-06-03 00:00:00 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: polyrex
|
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:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: dynarex
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description:
|
36
|
+
email:
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files: []
|
42
|
+
|
43
|
+
files:
|
44
|
+
- lib/dynarex-blog.rb
|
45
|
+
has_rdoc: true
|
46
|
+
homepage:
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.5
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: dynarex-blog
|
73
|
+
test_files: []
|
74
|
+
|