review-peg 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +36 -0
- data/.rubocop.yml +47 -0
- data/.rubocop_todo.yml +605 -0
- data/.travis.yml +18 -0
- data/COPYING +515 -0
- data/ChangeLog +2449 -0
- data/Dockerfile +22 -0
- data/Gemfile +6 -0
- data/README.rdoc +81 -0
- data/Rakefile +51 -0
- data/bin/review-catalog-converter-peg +129 -0
- data/bin/review-check-peg +169 -0
- data/bin/review-checkdep-peg +63 -0
- data/bin/review-compile-peg +202 -0
- data/bin/review-epubmaker-legacy-peg +1024 -0
- data/bin/review-epubmaker-peg +44 -0
- data/bin/review-index-peg +110 -0
- data/bin/review-init-peg +151 -0
- data/bin/review-pdfmaker-peg +18 -0
- data/bin/review-preproc-peg +131 -0
- data/bin/review-validate-peg +51 -0
- data/bin/review-vol-peg +100 -0
- data/debian/README.Debian +12 -0
- data/debian/README.source +5 -0
- data/debian/changelog +5 -0
- data/debian/compat +1 -0
- data/debian/control +22 -0
- data/debian/copyright +62 -0
- data/debian/docs +6 -0
- data/debian/manpage.1.ex +59 -0
- data/debian/patches/path.diff +91 -0
- data/debian/patches/series +1 -0
- data/debian/review.install +13 -0
- data/debian/review.links +4 -0
- data/debian/rules +13 -0
- data/debian/source/format +1 -0
- data/doc/NEWS.ja.md +350 -0
- data/doc/NEWS.md +354 -0
- data/doc/catalog.ja.md +53 -0
- data/doc/catalog.md +52 -0
- data/doc/format.ja.md +734 -0
- data/doc/format.md +746 -0
- data/doc/format_idg.ja.md +203 -0
- data/doc/quickstart.ja.md +222 -0
- data/doc/quickstart.md +252 -0
- data/doc/ruby-uuid/README +11 -0
- data/doc/ruby-uuid/README.ja +34 -0
- data/doc/sample.css +108 -0
- data/doc/sample.yml +238 -0
- data/lib/epubmaker.rb +24 -0
- data/lib/epubmaker/content.rb +93 -0
- data/lib/epubmaker/epubcommon.rb +424 -0
- data/lib/epubmaker/epubv2.rb +139 -0
- data/lib/epubmaker/epubv3.rb +222 -0
- data/lib/epubmaker/producer.rb +330 -0
- data/lib/lineinput.rb +107 -0
- data/lib/review.rb +3 -0
- data/lib/review/book.rb +43 -0
- data/lib/review/book/base.rb +401 -0
- data/lib/review/book/chapter.rb +100 -0
- data/lib/review/book/compilable.rb +184 -0
- data/lib/review/book/image_finder.rb +71 -0
- data/lib/review/book/index.rb +413 -0
- data/lib/review/book/page_metric.rb +47 -0
- data/lib/review/book/part.rb +54 -0
- data/lib/review/book/volume.rb +67 -0
- data/lib/review/builder.rb +452 -0
- data/lib/review/catalog.rb +52 -0
- data/lib/review/compiler.rb +5183 -0
- data/lib/review/compiler/literals_1_9.kpeg +22 -0
- data/lib/review/compiler/literals_1_9.rb +435 -0
- data/lib/review/configure.rb +64 -0
- data/lib/review/epubbuilder.rb +18 -0
- data/lib/review/epubmaker.rb +480 -0
- data/lib/review/ewbbuilder.rb +381 -0
- data/lib/review/exception.rb +21 -0
- data/lib/review/extentions.rb +4 -0
- data/lib/review/extentions/array.rb +25 -0
- data/lib/review/extentions/object.rb +9 -0
- data/lib/review/extentions/string.rb +33 -0
- data/lib/review/htmlbuilder.rb +1166 -0
- data/lib/review/htmllayout.rb +41 -0
- data/lib/review/htmltoc.rb +45 -0
- data/lib/review/htmlutils.rb +90 -0
- data/lib/review/i18n.rb +96 -0
- data/lib/review/i18n.yml +169 -0
- data/lib/review/idgxmlbuilder.rb +1233 -0
- data/lib/review/inaobuilder.rb +357 -0
- data/lib/review/latexbuilder.rb +941 -0
- data/lib/review/latexindex.rb +35 -0
- data/lib/review/latexutils.rb +95 -0
- data/lib/review/layout.tex.erb +340 -0
- data/lib/review/lineinput.rb +17 -0
- data/lib/review/location.rb +24 -0
- data/lib/review/makerhelper.rb +67 -0
- data/lib/review/markdownbuilder.rb +339 -0
- data/lib/review/node.rb +288 -0
- data/lib/review/pdfmaker.rb +332 -0
- data/lib/review/preprocessor.rb +530 -0
- data/lib/review/review.kpeg +745 -0
- data/lib/review/sec_counter.rb +69 -0
- data/lib/review/template.rb +21 -0
- data/lib/review/textbuilder.rb +17 -0
- data/lib/review/textutils.rb +16 -0
- data/lib/review/tocparser.rb +348 -0
- data/lib/review/tocprinter.rb +205 -0
- data/lib/review/topbuilder.rb +796 -0
- data/lib/review/unfold.rb +138 -0
- data/lib/review/version.rb +3 -0
- data/lib/uuid.rb +312 -0
- data/review.gemspec +32 -0
- data/templates/html/layout-html5.html.erb +17 -0
- data/templates/html/layout-xhtml1.html.erb +20 -0
- data/templates/ncx/epubv2.ncx.erb +11 -0
- data/templates/opf/epubv2.opf.erb +21 -0
- data/templates/opf/epubv3.opf.erb +18 -0
- data/templates/xml/container.xml.erb +6 -0
- data/test/CHAPS +2 -0
- data/test/assets/test.xml.erb +3 -0
- data/test/assets/test_template.tex +255 -0
- data/test/assets/test_template_backmatter.tex +32 -0
- data/test/bib.re +13 -0
- data/test/book_test_helper.rb +35 -0
- data/test/sample-book/README.md +7 -0
- data/test/sample-book/src/Rakefile +58 -0
- data/test/sample-book/src/_cover.html +3 -0
- data/test/sample-book/src/catalog.yml +10 -0
- data/test/sample-book/src/ch01.re +71 -0
- data/test/sample-book/src/ch02.re +3 -0
- data/test/sample-book/src/config.yml +186 -0
- data/test/sample-book/src/images/ch01-imgsample.jpg +0 -0
- data/test/sample-book/src/images/cover.jpg +0 -0
- data/test/sample-book/src/preface.re +15 -0
- data/test/sample-book/src/sty/jumoline.sty +310 -0
- data/test/sample-book/src/sty/reviewmacro.sty +39 -0
- data/test/sample-book/src/style.css +251 -0
- data/test/sample-book/src/vendor/jumoline/README +29 -0
- data/test/sample-book/src/vendor/jumoline/jumoline.dtx +2988 -0
- data/test/sample-book/src/vendor/jumoline/jumoline.ins +6 -0
- data/test/test.re +43 -0
- data/test/test_book.rb +556 -0
- data/test/test_book_chapter.rb +280 -0
- data/test/test_book_part.rb +54 -0
- data/test/test_builder.rb +80 -0
- data/test/test_catalog.rb +119 -0
- data/test/test_catalog_converter_cmd.rb +73 -0
- data/test/test_compiler.rb +92 -0
- data/test/test_configure.rb +50 -0
- data/test/test_epub3maker.rb +529 -0
- data/test/test_epubmaker.rb +569 -0
- data/test/test_epubmaker_cmd.rb +40 -0
- data/test/test_helper.rb +92 -0
- data/test/test_htmlbuilder.rb +1114 -0
- data/test/test_htmltoc.rb +32 -0
- data/test/test_htmlutils.rb +50 -0
- data/test/test_i18n.rb +180 -0
- data/test/test_idgxmlbuilder.rb +608 -0
- data/test/test_image_finder.rb +82 -0
- data/test/test_inaobuilder.rb +245 -0
- data/test/test_index.rb +174 -0
- data/test/test_latexbuilder.rb +732 -0
- data/test/test_lineinput.rb +182 -0
- data/test/test_makerhelper.rb +66 -0
- data/test/test_markdownbuilder.rb +125 -0
- data/test/test_pdfmaker.rb +171 -0
- data/test/test_pdfmaker_cmd.rb +40 -0
- data/test/test_preprocessor.rb +23 -0
- data/test/test_review_ext.rb +31 -0
- data/test/test_template.rb +26 -0
- data/test/test_textutils.rb +32 -0
- data/test/test_topbuilder.rb +291 -0
- data/test/test_uuid.rb +157 -0
- metadata +357 -0
@@ -0,0 +1,138 @@
|
|
1
|
+
#
|
2
|
+
# $Id: unfold.rb 3878 2008-02-09 13:12:15Z aamine $
|
3
|
+
#
|
4
|
+
# Copyright (c) 2002-2007 Minero Aoki
|
5
|
+
#
|
6
|
+
# This program is free software.
|
7
|
+
# You can distribute or modify this program under the terms of
|
8
|
+
# the GNU LGPL, Lesser General Public License version 2.1.
|
9
|
+
# For details of LGPL, see the file "COPYING".
|
10
|
+
#
|
11
|
+
|
12
|
+
require 'review/preprocessor'
|
13
|
+
require 'stringio'
|
14
|
+
|
15
|
+
module ReVIEW
|
16
|
+
|
17
|
+
class WrongInput < Error; end
|
18
|
+
|
19
|
+
class Unfold
|
20
|
+
|
21
|
+
# unfold paragraphs and strip preprocessor tags.
|
22
|
+
def Unfold.unfold_author_source(s)
|
23
|
+
unfold(Preprocessor::Strip.new(StringIO.new(s)))
|
24
|
+
end
|
25
|
+
|
26
|
+
def Unfold.unfold(f)
|
27
|
+
new().unfold(f)
|
28
|
+
end
|
29
|
+
|
30
|
+
def initialize(indent_paragraph = false)
|
31
|
+
@indent_paragraph = indent_paragraph
|
32
|
+
end
|
33
|
+
|
34
|
+
# unfold(f) -> String
|
35
|
+
# unfold(input, output) -> nil
|
36
|
+
def unfold(input, output = nil)
|
37
|
+
if output
|
38
|
+
@output = output
|
39
|
+
do_unfold input
|
40
|
+
nil
|
41
|
+
else
|
42
|
+
@output = StringIO.new
|
43
|
+
do_unfold input
|
44
|
+
@output.string
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
ZSPACE = "\241\241" # EUC-JP zen-kaku space
|
51
|
+
|
52
|
+
def do_unfold(input)
|
53
|
+
@blank_needed = false
|
54
|
+
first = true
|
55
|
+
indent = @indent_paragraph ? ZSPACE : ''
|
56
|
+
f = LineInput.new(input)
|
57
|
+
while line = f.gets
|
58
|
+
case line
|
59
|
+
when /\A\#@/
|
60
|
+
raise "must not happen: input includes preproc directive: #{line.inspect}"
|
61
|
+
when /\A=/
|
62
|
+
if first
|
63
|
+
first = false
|
64
|
+
else
|
65
|
+
blank
|
66
|
+
end
|
67
|
+
println line
|
68
|
+
# blank
|
69
|
+
when /\A\s+\*/
|
70
|
+
blank
|
71
|
+
println line
|
72
|
+
skip_block f, /\A\s+\*|\A\s+\S/
|
73
|
+
blank
|
74
|
+
when /\A\s+\d+\./
|
75
|
+
blank
|
76
|
+
println line
|
77
|
+
skip_block f, /\A\s+\d+\.|\A\s+\S/
|
78
|
+
blank
|
79
|
+
when /\A:/
|
80
|
+
blank
|
81
|
+
println line
|
82
|
+
skip_block f, /\A:|\A\s+\S/
|
83
|
+
blank
|
84
|
+
when %r<\A//\w.*\{\s*\z>
|
85
|
+
blank
|
86
|
+
println line
|
87
|
+
f.until_terminator(%r<\A//\}>) do |s|
|
88
|
+
println s
|
89
|
+
end
|
90
|
+
println '//}'
|
91
|
+
blank
|
92
|
+
when %r<\A//\w>
|
93
|
+
blank
|
94
|
+
println line
|
95
|
+
blank
|
96
|
+
when /\A\S/
|
97
|
+
if %r<\A//\[> =~ line
|
98
|
+
$stderr.puts "warning: #{f.path}:#{f.lineno}: paragraph begin with `//['; missing ReVIEW directive name?"
|
99
|
+
end
|
100
|
+
flush_blank
|
101
|
+
@output.print indent + line.rstrip
|
102
|
+
f.until_match(%r<\A\s*\z|\A//\w>) do |s|
|
103
|
+
@output.print s.rstrip
|
104
|
+
end
|
105
|
+
@output.puts
|
106
|
+
else
|
107
|
+
unless line.strip.empty?
|
108
|
+
raise WrongInput, "#{f.path}:#{f.lineno}: wrong input: #{line.inspect}"
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def skip_block(f, re)
|
115
|
+
f.while_match(re) do |line|
|
116
|
+
@output.puts line.rstrip
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
def blank
|
121
|
+
@blank_needed = true
|
122
|
+
end
|
123
|
+
|
124
|
+
def println(s)
|
125
|
+
flush_blank
|
126
|
+
@output.puts s.rstrip
|
127
|
+
end
|
128
|
+
|
129
|
+
def flush_blank
|
130
|
+
if @blank_needed
|
131
|
+
@output.puts
|
132
|
+
@blank_needed = false
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
137
|
+
|
138
|
+
end # module ReVIEW
|
data/lib/uuid.rb
ADDED
@@ -0,0 +1,312 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# Copyright(c) 2005 URABE, Shyouhei.
|
3
|
+
#
|
4
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
5
|
+
# of this code, to deal in the code without restriction, including without
|
6
|
+
# limitation the rights to use, copy, modify, merge, publish, distribute,
|
7
|
+
# sublicense, and/or sell copies of the code, and to permit persons to whom the
|
8
|
+
# code is furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be
|
11
|
+
# included in all copies or substantial portions of the code.
|
12
|
+
#
|
13
|
+
# THE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHOR OR COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE CODE OR THE USE OR OTHER DEALINGS IN THE
|
19
|
+
# CODE.
|
20
|
+
|
21
|
+
%w[
|
22
|
+
digest/md5
|
23
|
+
digest/sha1
|
24
|
+
tmpdir
|
25
|
+
].each do |f|
|
26
|
+
require f
|
27
|
+
end
|
28
|
+
|
29
|
+
# Pure ruby UUID generator, which is compatible with RFC4122
|
30
|
+
class UUID
|
31
|
+
# UUID epoch is 15th Oct. 1582
|
32
|
+
UNIXEpoch = 0x01B21DD213814000 # in 100-nanoseconds resolution
|
33
|
+
|
34
|
+
private_class_method :new
|
35
|
+
|
36
|
+
private
|
37
|
+
def initialize str
|
38
|
+
tmp = str.unpack "C*"
|
39
|
+
@num = tmp.inject do |r, i|
|
40
|
+
r * 256 | i
|
41
|
+
end
|
42
|
+
@num.freeze
|
43
|
+
self.freeze
|
44
|
+
end
|
45
|
+
|
46
|
+
public
|
47
|
+
|
48
|
+
def raw_bytes
|
49
|
+
ret = String.new
|
50
|
+
tmp = @num
|
51
|
+
16.times do |i|
|
52
|
+
x, y = tmp.divmod 256
|
53
|
+
ret << y
|
54
|
+
tmp = x
|
55
|
+
end
|
56
|
+
ret.reverse!
|
57
|
+
ret
|
58
|
+
end
|
59
|
+
|
60
|
+
class << self
|
61
|
+
def mask ver, str # :nodoc:
|
62
|
+
ver = ver & 15
|
63
|
+
v = str[6].ord
|
64
|
+
v &= 0b0000_1111
|
65
|
+
v |= ver << 4
|
66
|
+
str[6] = v.chr
|
67
|
+
r = str[8].ord
|
68
|
+
r &= 0b0011_1111
|
69
|
+
r |= 0b1000_0000
|
70
|
+
str[8] = r.chr
|
71
|
+
str
|
72
|
+
end
|
73
|
+
|
74
|
+
def prand # :nodoc:
|
75
|
+
rand 0x100000000
|
76
|
+
end
|
77
|
+
|
78
|
+
private :mask, :prand
|
79
|
+
|
80
|
+
# UUID generation using SHA1. Recommended over create_md5.
|
81
|
+
# Namespace object is another UUID, some of them are pre-defined below.
|
82
|
+
def create_sha1 str, namespace
|
83
|
+
sha1 = Digest::SHA1.new
|
84
|
+
sha1.update namespace.raw_bytes
|
85
|
+
sha1.update str
|
86
|
+
sum = sha1.digest
|
87
|
+
raw = mask 5, sum[0..15]
|
88
|
+
new raw
|
89
|
+
end
|
90
|
+
|
91
|
+
# UUID generation using MD5 (for backward compat.)
|
92
|
+
def create_md5 str, namespace
|
93
|
+
md5 = Digest::MD5.new
|
94
|
+
md5.update namespace.raw_bytes
|
95
|
+
md5.update str
|
96
|
+
sum = md5.digest
|
97
|
+
raw = mask 3, sum[0..16]
|
98
|
+
new raw
|
99
|
+
end
|
100
|
+
|
101
|
+
# UUID generation using random-number generator. From it's random
|
102
|
+
# nature, there's no warranty that the created ID is really universaly
|
103
|
+
# unique.
|
104
|
+
def create_random
|
105
|
+
rnd = [prand, prand, prand, prand].pack "N4"
|
106
|
+
raw = mask 4, rnd
|
107
|
+
new raw
|
108
|
+
end
|
109
|
+
|
110
|
+
def read_state fp # :nodoc:
|
111
|
+
fp.rewind
|
112
|
+
Marshal.load fp.read
|
113
|
+
end
|
114
|
+
|
115
|
+
def write_state fp, c, m # :nodoc:
|
116
|
+
fp.rewind
|
117
|
+
str = Marshal.dump [c, m]
|
118
|
+
fp.write str
|
119
|
+
end
|
120
|
+
|
121
|
+
private :read_state, :write_state
|
122
|
+
STATE_FILE = 'ruby-uuid'
|
123
|
+
|
124
|
+
# create the "version 1" UUID with current system clock, current UTC
|
125
|
+
# timestamp, and the IEEE 802 address (so-called MAC address).
|
126
|
+
#
|
127
|
+
# Speed notice: it's slow. It writes some data into hard drive on every
|
128
|
+
# invokation. If you want to speed this up, try remounting tmpdir with a
|
129
|
+
# memory based filesystem (such as tmpfs). STILL slow? then no way but
|
130
|
+
# rewrite it with c :)
|
131
|
+
def create clock=nil, time=Time.now, mac_addr=nil
|
132
|
+
c = t = m = nil
|
133
|
+
Dir.chdir Dir.tmpdir do
|
134
|
+
unless FileTest.exist? STATE_FILE then
|
135
|
+
# Generate a pseudo MAC address because we have no pure-ruby way
|
136
|
+
# to know the MAC address of the NIC this system uses. Note
|
137
|
+
# that cheating with pseudo arresses here is completely legal:
|
138
|
+
# see Section 4.5 of RFC4122 for details.
|
139
|
+
sha1 = Digest::SHA1.new
|
140
|
+
256.times do
|
141
|
+
r = [prand].pack "N"
|
142
|
+
sha1.update r
|
143
|
+
end
|
144
|
+
ary = sha1.digest.bytes.to_a
|
145
|
+
node = ary.last 6
|
146
|
+
node[0] |= 0x01 # multicast bit
|
147
|
+
node = node.pack "C*"
|
148
|
+
k = rand 0x40000
|
149
|
+
open STATE_FILE, 'w' do |fp|
|
150
|
+
fp.flock IO::LOCK_EX
|
151
|
+
write_state fp, k, node
|
152
|
+
fp.chmod 0o777 # must be world writable
|
153
|
+
end
|
154
|
+
end
|
155
|
+
open STATE_FILE, 'r+' do |fp|
|
156
|
+
fp.flock IO::LOCK_EX
|
157
|
+
c, m = read_state fp
|
158
|
+
c += 1 # important; increment here
|
159
|
+
write_state fp, c, m
|
160
|
+
end
|
161
|
+
end
|
162
|
+
c = clock & 0b11_1111_1111_1111 if clock
|
163
|
+
m = mac_addr if mac_addr
|
164
|
+
time = Time.at time if time.is_a? Float
|
165
|
+
case time
|
166
|
+
when Time
|
167
|
+
t = time.to_i * 10_000_000 + time.tv_usec * 10 + UNIXEpoch
|
168
|
+
when Integer
|
169
|
+
t = time + UNIXEpoch
|
170
|
+
else
|
171
|
+
raise TypeError, "cannot convert ``#{time}'' into Time."
|
172
|
+
end
|
173
|
+
|
174
|
+
tl = t & 0xFFFF_FFFF
|
175
|
+
tm = t >> 32
|
176
|
+
tm = tm & 0xFFFF
|
177
|
+
th = t >> 48
|
178
|
+
th = th & 0b0000_1111_1111_1111
|
179
|
+
th = th | 0b0001_0000_0000_0000
|
180
|
+
cl = c & 0b0000_0000_1111_1111
|
181
|
+
ch = c & 0b0011_1111_0000_0000
|
182
|
+
ch = ch >> 8
|
183
|
+
ch = ch | 0b1000_0000
|
184
|
+
pack tl, tm, th, ch, cl, m
|
185
|
+
end
|
186
|
+
|
187
|
+
# A simple GUID parser: just ignores unknown characters and convert
|
188
|
+
# hexadecimal dump into 16-octet object.
|
189
|
+
def parse obj
|
190
|
+
str = obj.to_s.sub %r/\Aurn:uuid:/, ''
|
191
|
+
str.gsub! %r/[^0-9A-Fa-f]/, ''
|
192
|
+
raw = [str[0..31]].pack 'H*'
|
193
|
+
new raw
|
194
|
+
end
|
195
|
+
|
196
|
+
# The 'primitive constructor' of this class
|
197
|
+
# Note UUID.pack(uuid.unpack) == uuid
|
198
|
+
def pack tl, tm, th, ch, cl, n
|
199
|
+
raw = [tl, tm, th, ch, cl, n].pack "NnnCCa6"
|
200
|
+
new raw
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
# The 'primitive deconstructor', or the dual to pack.
|
205
|
+
# Note UUID.pack(uuid.unpack) == uuid
|
206
|
+
def unpack
|
207
|
+
raw_bytes.unpack "NnnCCa6"
|
208
|
+
end
|
209
|
+
|
210
|
+
# The timestamp of this UUID.
|
211
|
+
# Throws RageError if that time exceeds UNIX time range
|
212
|
+
def time
|
213
|
+
a = unpack
|
214
|
+
tl = a[0]
|
215
|
+
tm = a[1]
|
216
|
+
th = a[2] & 0x0FFF
|
217
|
+
t = tl
|
218
|
+
t += tm << 32
|
219
|
+
t += th << 48
|
220
|
+
t -= UNIXEpoch
|
221
|
+
tv_sec = t / 10_000_000
|
222
|
+
t -= tv_sec * 10_000_000
|
223
|
+
tv_usec = t / 10
|
224
|
+
Time.at tv_sec, tv_usec
|
225
|
+
end
|
226
|
+
|
227
|
+
# The version of this UUID
|
228
|
+
def version
|
229
|
+
v = unpack[2] & 0b1111_0000_0000_0000
|
230
|
+
v >> 12
|
231
|
+
end
|
232
|
+
|
233
|
+
# The clock sequence of this UUID
|
234
|
+
def clock
|
235
|
+
a = unpack
|
236
|
+
ch = a[3] & 0b0001_1111
|
237
|
+
cl = a[4]
|
238
|
+
c = cl
|
239
|
+
c += ch << 8
|
240
|
+
c
|
241
|
+
end
|
242
|
+
|
243
|
+
# The IEEE 802 address in a hexadecimal format
|
244
|
+
def node
|
245
|
+
m = unpack[5].unpack 'C*'
|
246
|
+
'%02x%02x%02x%02x%02x%02x' % m
|
247
|
+
end
|
248
|
+
alias_method :mac_address, :node
|
249
|
+
alias_method :ieee802, :node
|
250
|
+
|
251
|
+
# Generate the string representation (a.k.a GUID) of this UUID
|
252
|
+
def to_s
|
253
|
+
a = unpack
|
254
|
+
a[-1] = mac_address
|
255
|
+
"%08x-%04x-%04x-%02x%02x-%s" % a
|
256
|
+
end
|
257
|
+
alias_method :guid, :to_s
|
258
|
+
|
259
|
+
# Convert into a RFC4122-comforming URN representation
|
260
|
+
def to_uri
|
261
|
+
"urn:uuid:" + self.to_s
|
262
|
+
end
|
263
|
+
alias_method :urn, :to_uri
|
264
|
+
alias_method :inspect, :to_uri
|
265
|
+
|
266
|
+
# Convert into 128-bit unsigned integer
|
267
|
+
# Typically a Bignum instance, but can be a Fixnum.
|
268
|
+
def to_int
|
269
|
+
@num
|
270
|
+
end
|
271
|
+
alias_method :to_i, :to_int
|
272
|
+
|
273
|
+
# Two UUIDs are said to be equal if and only if their (byte-order
|
274
|
+
# canonicalized) integer representations are equivallent. Refer RFC4122 for
|
275
|
+
# details.
|
276
|
+
def == other
|
277
|
+
to_i == other.to_i
|
278
|
+
end
|
279
|
+
alias_method :eql?, :==
|
280
|
+
|
281
|
+
# Two identical UUIDs should have same hash
|
282
|
+
def hash
|
283
|
+
to_i
|
284
|
+
end
|
285
|
+
|
286
|
+
include Comparable
|
287
|
+
# UUIDs are comparable (don't know what benefits are there, though).
|
288
|
+
def <=> other
|
289
|
+
to_s <=> other.to_s
|
290
|
+
end
|
291
|
+
|
292
|
+
# Pre-defined UUID Namespaces described in RFC4122 Appendix C.
|
293
|
+
NameSpace_DNS = parse "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
|
294
|
+
NameSpace_URL = parse "6ba7b811-9dad-11d1-80b4-00c04fd430c8"
|
295
|
+
NameSpace_OID = parse "6ba7b812-9dad-11d1-80b4-00c04fd430c8"
|
296
|
+
NameSpace_X500 = parse "6ba7b814-9dad-11d1-80b4-00c04fd430c8"
|
297
|
+
|
298
|
+
# The Nil UUID in RFC4122 Section 4.1.7
|
299
|
+
Nil = parse "00000000-0000-0000-0000-000000000000"
|
300
|
+
end
|
301
|
+
|
302
|
+
|
303
|
+
# Local Variables:
|
304
|
+
# mode: ruby
|
305
|
+
# coding: utf-8
|
306
|
+
# indent-tabs-mode: t
|
307
|
+
# tab-width: 3
|
308
|
+
# ruby-indent-level: 3
|
309
|
+
# fill-column: 79
|
310
|
+
# default-justification: full
|
311
|
+
# End:
|
312
|
+
# vi: ts=3 sw=3
|