bibmarkdown 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +9 -0
- data/LICENSE.md +9 -0
- data/README.md +72 -0
- data/Rakefile +23 -0
- data/VERSION +1 -0
- data/lib/bibmarkdown/document.rb +89 -0
- data/lib/bibmarkdown.rb +1 -0
- metadata +108 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 397abd2bd0e065e1c3402a3103e0febb7054df63
|
4
|
+
data.tar.gz: 4110f6df3c21c0bfca0f0da0fe9ba5b41f281d36
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2d6daf22e4101cf9738839346c0085280971b955e3606cf7530fdbfac7644e185bef23bbe59203c727421eb29b4bd2593465433e9a5070d0760f84ac5e76ab48
|
7
|
+
data.tar.gz: e599d213d2ee38aea9a4890267c443d838df048bb9fa1907374808cb217295881c9af62b23335cc24c796e324e7fc705b4befe635b28be926d28cbf2203b77c6
|
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
# License
|
2
|
+
The MIT License (MIT)
|
3
|
+
Copyright ©2016 Ruben Verborgh
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
6
|
+
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
8
|
+
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
## BibMarkown
|
2
|
+
BibMarkdown is a Ruby pre-processor for Markdown,
|
3
|
+
adding support for citations
|
4
|
+
realized by a BibTex back-end.
|
5
|
+
|
6
|
+
## Syntax and result
|
7
|
+
This Markdown snippet
|
8
|
+
```markdown
|
9
|
+
The way _clients_ and _servers_ exchange information on the Web
|
10
|
+
is modeled by the [REST architectural style](cite:citesAsAuthority REST).
|
11
|
+
```
|
12
|
+
will be processed into the following Markdown snippet:
|
13
|
+
```markdown
|
14
|
+
The way _clients_ and _servers_ exchange information on the Web
|
15
|
+
is modeled by the <a rel="http://purl.org/spar/cito/citesAsAuthority" href="http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm">REST architectural style</a> <a class="reference" href="#ref-1">[1]</a>.
|
16
|
+
```
|
17
|
+
|
18
|
+
This Markdown snippet
|
19
|
+
```markdown
|
20
|
+
The way _clients_ and _servers_ exchange information on the Web
|
21
|
+
is modeled by the REST architectural style [](cite:citesAsAuthority REST).
|
22
|
+
```
|
23
|
+
will be processed into the following Markdown snippet:
|
24
|
+
```markdown
|
25
|
+
The way _clients_ and _servers_ exchange information on the Web
|
26
|
+
is modeled by the REST architectural style <a class="reference" href="#ref-1">[1]</a>.
|
27
|
+
```
|
28
|
+
|
29
|
+
Furthermore, at the end of the Markdown document,
|
30
|
+
a _References_ section will be added:
|
31
|
+
```html
|
32
|
+
<h2 id="references">References</h2>
|
33
|
+
<dl class="references">
|
34
|
+
<dt id="ref-1">[1]</dt>
|
35
|
+
<dd>Fielding, R.T., 2000. Architectural Styles and the Design of Network-based Software Architectures (PhD thesis). University of California.</dd>
|
36
|
+
</dl>
|
37
|
+
```
|
38
|
+
|
39
|
+
## Usage
|
40
|
+
```ruby
|
41
|
+
require 'bibmarkdown'
|
42
|
+
require 'bibtex'
|
43
|
+
require 'csl/styles'
|
44
|
+
|
45
|
+
content = <<-md
|
46
|
+
The way _clients_ and _servers_ exchange information on the Web
|
47
|
+
is modeled by the [REST architectural style](cite:citesAsAuthority REST).
|
48
|
+
md
|
49
|
+
|
50
|
+
bibliography = <<-bib
|
51
|
+
@phdthesis{REST,
|
52
|
+
author = {Roy Thomas Fielding},
|
53
|
+
title = {Architectural Styles and the Design of Network-based Software Architectures},
|
54
|
+
school = {University of California},
|
55
|
+
year = 2000,
|
56
|
+
url = {http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm},
|
57
|
+
}
|
58
|
+
bib
|
59
|
+
|
60
|
+
entries = BibTeX.parse(bibliography).entries
|
61
|
+
document = BibMarkdown::Document.new content,
|
62
|
+
entries: entries, style: 'elsevier-harvard'
|
63
|
+
puts document.to_markdown
|
64
|
+
```
|
65
|
+
|
66
|
+
## Related work
|
67
|
+
This library was inspired by [Pieter Colpaert](http://pieter.pm/)'s
|
68
|
+
[jekyll-refs](https://github.com/pietercolpaert/jekyll-refs),
|
69
|
+
which has some nice [CSS suggestions](https://github.com/pietercolpaert/jekyll-refs#using-css-styles-to-mark-up-the-citations) as well.
|
70
|
+
|
71
|
+
## License
|
72
|
+
Copyright ©2016 Ruben Verborgh – MIT License
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'juwelier'
|
15
|
+
Juwelier::Tasks.new do |gem|
|
16
|
+
gem.name = "bibmarkdown"
|
17
|
+
gem.homepage = "http://github.com/RubenVerborgh/bibmarkdown"
|
18
|
+
gem.license = "MIT"
|
19
|
+
gem.summary = "Markdown with BibTeX citations."
|
20
|
+
gem.email = "ruben@verborgh.org"
|
21
|
+
gem.authors = ["Ruben Verborgh"]
|
22
|
+
end
|
23
|
+
Juwelier::RubygemsDotOrgTasks.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,89 @@
|
|
1
|
+
require 'cgi'
|
2
|
+
require 'citeproc'
|
3
|
+
require 'csl/styles'
|
4
|
+
|
5
|
+
module BibMarkdown
|
6
|
+
class Document
|
7
|
+
def initialize(source, options)
|
8
|
+
@source = source
|
9
|
+
@entries = options[:entries]
|
10
|
+
@style = options[:style]
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_markdown
|
14
|
+
reference_ids = {}
|
15
|
+
|
16
|
+
# Replace all citations by links
|
17
|
+
markdown = @source.gsub %r{\[([^\]]*)\]\(cite:(\w+)\s+([^\)]+)\)} do |match|
|
18
|
+
html = $1; rel = $2; key = $3
|
19
|
+
|
20
|
+
# Look up or assign reference ID
|
21
|
+
if reference_ids.has_key? key
|
22
|
+
reference_id = reference_ids[key]
|
23
|
+
else
|
24
|
+
reference_id = reference_ids[key] = reference_ids.length + 1
|
25
|
+
end
|
26
|
+
|
27
|
+
# Look up citation and its URL
|
28
|
+
entry = @entries[key]
|
29
|
+
raise "Failed to generate references: entry '#{key}' does not exist." unless entry
|
30
|
+
url = entry[:url] || ''
|
31
|
+
|
32
|
+
# Create the reference
|
33
|
+
reflink = create_link "[#{reference_id}]", "#ref-#{reference_id}", class: 'reference'
|
34
|
+
|
35
|
+
# If the text is empty, just output the reference
|
36
|
+
if html.empty?
|
37
|
+
reflink
|
38
|
+
# If there is no URL, just output the text with the reference
|
39
|
+
elsif url.empty?
|
40
|
+
"#{html} #{reflink}"
|
41
|
+
# Otherwise, output the link and the reference
|
42
|
+
else
|
43
|
+
"#{create_link html, url, rel: 'http://purl.org/spar/cito/' + rel} #{reflink}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Append the reference list to the text
|
48
|
+
"#{markdown}\n\n#{references_html reference_ids}".rstrip
|
49
|
+
end
|
50
|
+
|
51
|
+
protected
|
52
|
+
def h text
|
53
|
+
CGI::escapeHTML(text || '')
|
54
|
+
end
|
55
|
+
|
56
|
+
def create_link html, url, attrs = {}
|
57
|
+
attrs[:href] = url
|
58
|
+
attrs = attrs.map { |attr, value| %Q{#{attr}="#{h value}"} }
|
59
|
+
%Q{<a #{attrs.join ' '}>#{html}</a>}
|
60
|
+
end
|
61
|
+
|
62
|
+
def references_html reference_ids
|
63
|
+
if reference_ids.empty?
|
64
|
+
''
|
65
|
+
else
|
66
|
+
html = %Q{<h2 id="references">References</h2>\n}
|
67
|
+
html += %Q{<dl class="references">\n}
|
68
|
+
reference_ids.each do |key, id|
|
69
|
+
html += %Q{ <dt id="ref-#{id}">[#{id}]</dt>\n}
|
70
|
+
html += %Q{ <dd>#{reference_html key}</dd>\n}
|
71
|
+
end
|
72
|
+
html += %Q{</dl>\n}
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def reference_html key
|
77
|
+
# Render reference
|
78
|
+
processor = CiteProc::Processor.new style: @style, format: 'html'
|
79
|
+
processor << @entries[key].to_citeproc
|
80
|
+
citations = processor.render :bibliography, id: key
|
81
|
+
citation = citations.first
|
82
|
+
|
83
|
+
# Replace URLs by links
|
84
|
+
citation.gsub %r{https?://[^ ]+[^ .]} do |match|
|
85
|
+
create_link h(match), match
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
data/lib/bibmarkdown.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'bibmarkdown/document'
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bibmarkdown
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ruben Verborgh
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: citeproc-ruby
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: csl-styles
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: juwelier
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.1.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.1.0
|
69
|
+
description:
|
70
|
+
email: ruben@verborgh.org
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files:
|
74
|
+
- LICENSE.md
|
75
|
+
- README.md
|
76
|
+
files:
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.md
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- VERSION
|
82
|
+
- lib/bibmarkdown.rb
|
83
|
+
- lib/bibmarkdown/document.rb
|
84
|
+
homepage: http://github.com/RubenVerborgh/bibmarkdown
|
85
|
+
licenses:
|
86
|
+
- MIT
|
87
|
+
metadata: {}
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 2.4.5.1
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: Markdown with BibTeX citations.
|
108
|
+
test_files: []
|