btparse-ruby 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +26 -0
- data/README +26 -0
- data/ext/btparse-ruby/btparse_ruby.c +163 -0
- data/ext/btparse-ruby/extconf.rb +11 -0
- data/lib/btparse-ruby.rb +2 -0
- data/lib/btparse-ruby/BibTeX.rb +23 -0
- metadata +72 -0
data/LICENSE
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
Copyright (c) 2010, Cory Cornelius <dxoigmn@gmail.com>.
|
2
|
+
|
3
|
+
All rights reserved.
|
4
|
+
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
6
|
+
modification, are permitted provided that the following conditions
|
7
|
+
are met:
|
8
|
+
|
9
|
+
1. Redistributions of source code must retain the above copyright
|
10
|
+
notice, this list of conditions and the following disclaimer.
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright
|
12
|
+
notice, this list of conditions and the following disclaimer in the
|
13
|
+
documentation and/or other materials provided with the distribution.
|
14
|
+
3. The name of the author may not be used to endorse or promote products
|
15
|
+
derived from this software without specific prior written permission.
|
16
|
+
|
17
|
+
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
18
|
+
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
19
|
+
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
20
|
+
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
21
|
+
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
22
|
+
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
23
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
24
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
25
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
26
|
+
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
btparse-ruby
|
2
|
+
============
|
3
|
+
|
4
|
+
btparse-ruby is a ruby wrapper for btparse.
|
5
|
+
|
6
|
+
http://www.gerg.ca/software/btOOL/
|
7
|
+
|
8
|
+
For now, btparse-ruby ignores comments, preambles, and strings. It does,
|
9
|
+
however, expand strings with macros. There are also methods to split author
|
10
|
+
lists and purify strings.
|
11
|
+
|
12
|
+
Example Usage
|
13
|
+
-------------
|
14
|
+
|
15
|
+
require 'btparse-ruby'
|
16
|
+
|
17
|
+
entries = BibTeX.parse(file)
|
18
|
+
|
19
|
+
entries.each do |entry|
|
20
|
+
puts entry.type
|
21
|
+
puts entry.key
|
22
|
+
|
23
|
+
entry.fields.each do |name, value|
|
24
|
+
puts " #{name} = #{value}"
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,163 @@
|
|
1
|
+
#include "ruby.h"
|
2
|
+
#include "btparse.h"
|
3
|
+
|
4
|
+
static VALUE rb_cBibTeX;
|
5
|
+
static VALUE rb_cEntry;
|
6
|
+
|
7
|
+
VALUE ast_to_class(AST *entries)
|
8
|
+
{
|
9
|
+
VALUE rb_bibtex = rb_class_new_instance(0, NULL, rb_cBibTeX);
|
10
|
+
VALUE rb_entries = rb_ary_new();
|
11
|
+
|
12
|
+
AST *entry = NULL;
|
13
|
+
while (entry = bt_next_entry(entries, entry))
|
14
|
+
{
|
15
|
+
/* Skip non-regular entries */
|
16
|
+
if (bt_entry_metatype(entry) != BTE_REGULAR) {
|
17
|
+
continue;
|
18
|
+
}
|
19
|
+
|
20
|
+
VALUE rb_entry = rb_class_new_instance(0, NULL, rb_cEntry);
|
21
|
+
|
22
|
+
/* Set Entry type */
|
23
|
+
char *entry_type = bt_entry_type(entry);
|
24
|
+
if (entry_type != NULL)
|
25
|
+
{
|
26
|
+
rb_iv_set(rb_entry, "@type", rb_str_new2(entry_type));
|
27
|
+
}
|
28
|
+
|
29
|
+
/* Set Entry key */
|
30
|
+
char *entry_key = bt_entry_key(entry);
|
31
|
+
if (entry_key != NULL)
|
32
|
+
{
|
33
|
+
rb_iv_set(rb_entry, "@key", rb_str_new2(entry_key));
|
34
|
+
}
|
35
|
+
|
36
|
+
/* Set Entry fields */
|
37
|
+
VALUE rb_fields = rb_hash_new();
|
38
|
+
char *name = NULL;
|
39
|
+
|
40
|
+
AST *field = NULL;
|
41
|
+
while (field = bt_next_field(entry, field, &name))
|
42
|
+
{
|
43
|
+
char *value = bt_get_text(field);
|
44
|
+
|
45
|
+
VALUE rb_name = rb_str_new2(name);
|
46
|
+
VALUE rb_value = rb_str_new2(value);
|
47
|
+
|
48
|
+
rb_hash_aset(rb_fields, rb_name, rb_value);
|
49
|
+
}
|
50
|
+
|
51
|
+
rb_iv_set(rb_entry, "@fields", rb_fields);
|
52
|
+
|
53
|
+
rb_ary_push(rb_entries, rb_entry);
|
54
|
+
}
|
55
|
+
|
56
|
+
rb_iv_set(rb_bibtex, "@entries", rb_entries);
|
57
|
+
|
58
|
+
return rb_bibtex;
|
59
|
+
}
|
60
|
+
|
61
|
+
|
62
|
+
static VALUE bibtex_parse(VALUE klass, VALUE rb_entry_text)
|
63
|
+
{
|
64
|
+
Check_Type(rb_entry_text, T_STRING);
|
65
|
+
|
66
|
+
char *entry_text = RSTRING_PTR(rb_entry_text);
|
67
|
+
boolean status = 0;
|
68
|
+
|
69
|
+
bt_initialize();
|
70
|
+
|
71
|
+
AST *entries = bt_parse_entry_s(entry_text, NULL, 1, 0, &status);
|
72
|
+
|
73
|
+
/* Exit early if parsing failed */
|
74
|
+
if (entries == NULL || status == 0)
|
75
|
+
{
|
76
|
+
return Qnil;
|
77
|
+
}
|
78
|
+
|
79
|
+
VALUE rb_entries = ast_to_class(entries);
|
80
|
+
|
81
|
+
bt_parse_entry_s(NULL, NULL, 1, 0, NULL);
|
82
|
+
|
83
|
+
bt_cleanup();
|
84
|
+
|
85
|
+
return rb_entries;
|
86
|
+
}
|
87
|
+
|
88
|
+
static VALUE bibtex_parse_file(VALUE klass, VALUE rb_filename)
|
89
|
+
{
|
90
|
+
Check_Type(rb_filename, T_STRING);
|
91
|
+
|
92
|
+
char *filename = RSTRING_PTR(rb_filename);
|
93
|
+
boolean status = 0;
|
94
|
+
|
95
|
+
bt_initialize();
|
96
|
+
|
97
|
+
AST *entries = bt_parse_file(filename, 0, &status);
|
98
|
+
|
99
|
+
/* Exit early if parsing failed */
|
100
|
+
if (entries == NULL || status == 0)
|
101
|
+
{
|
102
|
+
return Qnil;
|
103
|
+
}
|
104
|
+
|
105
|
+
VALUE rb_entries = ast_to_class(entries);
|
106
|
+
|
107
|
+
bt_cleanup();
|
108
|
+
|
109
|
+
return rb_entries;
|
110
|
+
}
|
111
|
+
|
112
|
+
|
113
|
+
static VALUE bibtex_parse_author(VALUE klass, VALUE rb_string)
|
114
|
+
{
|
115
|
+
Check_Type(rb_string, T_STRING);
|
116
|
+
|
117
|
+
bt_initialize();
|
118
|
+
|
119
|
+
bt_stringlist *authors = bt_split_list(RSTRING_PTR(rb_string), "and", NULL, 1, "authors");
|
120
|
+
|
121
|
+
VALUE rb_authors = rb_ary_new();
|
122
|
+
|
123
|
+
for (int i = 0; i < authors->num_items; i++) {
|
124
|
+
char *author = authors->items[i];
|
125
|
+
|
126
|
+
VALUE rb_author = rb_str_new2(author);
|
127
|
+
|
128
|
+
rb_ary_push(rb_authors, rb_author);
|
129
|
+
}
|
130
|
+
|
131
|
+
bt_free_list(authors);
|
132
|
+
|
133
|
+
bt_cleanup();
|
134
|
+
|
135
|
+
return rb_authors;
|
136
|
+
}
|
137
|
+
|
138
|
+
static VALUE bibtex_purify(VALUE klass, VALUE rb_string)
|
139
|
+
{
|
140
|
+
Check_Type(rb_string, T_STRING);
|
141
|
+
|
142
|
+
bt_initialize();
|
143
|
+
|
144
|
+
char *string = malloc(RSTRING_LEN(rb_string) + 1);
|
145
|
+
|
146
|
+
strcpy(string, RSTRING_PTR(rb_string));
|
147
|
+
bt_purify_string(string, 0);
|
148
|
+
|
149
|
+
bt_cleanup();
|
150
|
+
|
151
|
+
return rb_str_new2(string);
|
152
|
+
}
|
153
|
+
|
154
|
+
void Init_btparse_ruby()
|
155
|
+
{
|
156
|
+
rb_cBibTeX = rb_define_class("BibTeX", rb_cObject);
|
157
|
+
rb_define_singleton_method(rb_cBibTeX, "parse", RUBY_METHOD_FUNC(bibtex_parse), 1);
|
158
|
+
rb_define_singleton_method(rb_cBibTeX, "parse_file", RUBY_METHOD_FUNC(bibtex_parse_file), 1);
|
159
|
+
rb_define_singleton_method(rb_cBibTeX, "parse_author", RUBY_METHOD_FUNC(bibtex_parse_author), 1);
|
160
|
+
rb_define_singleton_method(rb_cBibTeX, "purify", RUBY_METHOD_FUNC(bibtex_purify), 1);
|
161
|
+
|
162
|
+
rb_cEntry = rb_define_class_under(rb_cBibTeX, "Entry", rb_cObject);
|
163
|
+
}
|
data/lib/btparse-ruby.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
class BibTeX
|
2
|
+
attr_reader :entries
|
3
|
+
end
|
4
|
+
|
5
|
+
class BibTeX::Entry
|
6
|
+
attr_reader :type, :key, :fields
|
7
|
+
|
8
|
+
def to_s
|
9
|
+
str = "@#{type}{#{key},\n"
|
10
|
+
|
11
|
+
fields.each do |name, value|
|
12
|
+
str += "\t#{BibTeX.purify(name)} = "
|
13
|
+
if name == "author"
|
14
|
+
str += BibTeX.parse_author(value).inspect
|
15
|
+
else
|
16
|
+
str += "'" + BibTeX.purify(value) + "'"
|
17
|
+
end
|
18
|
+
str += "\n"
|
19
|
+
end
|
20
|
+
|
21
|
+
str += "}"
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: btparse-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 15
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
version: "0.2"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Cory Cornelius
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-08-16 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: A simple little BibTeX parser.
|
22
|
+
email:
|
23
|
+
- dxoigmn@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions:
|
27
|
+
- ext/btparse-ruby/extconf.rb
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- README
|
32
|
+
- LICENSE
|
33
|
+
- ext/btparse-ruby/btparse_ruby.c
|
34
|
+
- ext/btparse-ruby/extconf.rb
|
35
|
+
- lib/btparse-ruby.rb
|
36
|
+
- lib/btparse-ruby/BibTeX.rb
|
37
|
+
has_rdoc: true
|
38
|
+
homepage: http://github.com/dxoigmn/btparse-ruby
|
39
|
+
licenses: []
|
40
|
+
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
hash: 3
|
52
|
+
segments:
|
53
|
+
- 0
|
54
|
+
version: "0"
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
hash: 3
|
61
|
+
segments:
|
62
|
+
- 0
|
63
|
+
version: "0"
|
64
|
+
requirements: []
|
65
|
+
|
66
|
+
rubyforge_project:
|
67
|
+
rubygems_version: 1.3.7
|
68
|
+
signing_key:
|
69
|
+
specification_version: 3
|
70
|
+
summary: Ruby wrapper for btparse.
|
71
|
+
test_files: []
|
72
|
+
|