├── VERSION ├── .gitignore ├── lib ├── bibmarkdown.rb └── bibmarkdown │ └── document.rb ├── Gemfile ├── Rakefile ├── LICENSE.md ├── bibmarkdown.gemspec └── README.md /VERSION: -------------------------------------------------------------------------------- 1 | 2.0.0 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .bundle 2 | pkg 3 | Gemfile.lock 4 | -------------------------------------------------------------------------------- /lib/bibmarkdown.rb: -------------------------------------------------------------------------------- 1 | require 'bibmarkdown/document' 2 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | gem "citeproc-ruby" 4 | gem "csl-styles" 5 | 6 | group :development do 7 | gem "bundler", "~> 1.0" 8 | gem "juwelier", "~> 2.4.7" 9 | end 10 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 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 24 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 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. 10 | -------------------------------------------------------------------------------- /bibmarkdown.gemspec: -------------------------------------------------------------------------------- 1 | # Generated by juwelier 2 | # DO NOT EDIT THIS FILE DIRECTLY 3 | # Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec' 4 | # -*- encoding: utf-8 -*- 5 | # stub: bibmarkdown 2.0.0 ruby lib 6 | 7 | Gem::Specification.new do |s| 8 | s.name = "bibmarkdown".freeze 9 | s.version = "2.0.0" 10 | 11 | s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= 12 | s.require_paths = ["lib".freeze] 13 | s.authors = ["Ruben Verborgh".freeze] 14 | s.date = "2018-05-22" 15 | s.email = "ruben@verborgh.org".freeze 16 | s.extra_rdoc_files = [ 17 | "LICENSE.md", 18 | "README.md" 19 | ] 20 | s.files = [ 21 | "Gemfile", 22 | "LICENSE.md", 23 | "README.md", 24 | "Rakefile", 25 | "VERSION", 26 | "bibmarkdown.gemspec", 27 | "lib/bibmarkdown.rb", 28 | "lib/bibmarkdown/document.rb" 29 | ] 30 | s.homepage = "http://github.com/RubenVerborgh/bibmarkdown".freeze 31 | s.licenses = ["MIT".freeze] 32 | s.rubygems_version = "2.7.3".freeze 33 | s.summary = "Markdown with BibTeX citations.".freeze 34 | 35 | if s.respond_to? :specification_version then 36 | s.specification_version = 4 37 | 38 | if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then 39 | s.add_runtime_dependency(%q.freeze, [">= 0"]) 40 | s.add_runtime_dependency(%q.freeze, [">= 0"]) 41 | s.add_development_dependency(%q.freeze, ["~> 1.0"]) 42 | s.add_development_dependency(%q.freeze, ["~> 2.4.7"]) 43 | else 44 | s.add_dependency(%q.freeze, [">= 0"]) 45 | s.add_dependency(%q.freeze, [">= 0"]) 46 | s.add_dependency(%q.freeze, ["~> 1.0"]) 47 | s.add_dependency(%q.freeze, ["~> 2.4.7"]) 48 | end 49 | else 50 | s.add_dependency(%q.freeze, [">= 0"]) 51 | s.add_dependency(%q.freeze, [">= 0"]) 52 | s.add_dependency(%q.freeze, ["~> 1.0"]) 53 | s.add_dependency(%q.freeze, ["~> 2.4.7"]) 54 | end 55 | end 56 | 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## BibMarkown 2 | [![bibmarkdown gem version](https://badge.fury.io/rb/bibmarkdown.svg)](https://rubygems.org/gems/bibmarkdown) 3 | 4 | BibMarkdown is a Ruby pre-processor for Markdown, 5 | adding support for citations 6 | realized by a BibTex back-end. 7 | 8 | ## Syntax and result 9 | This Markdown snippet 10 | ```markdown 11 | The way _clients_ and _servers_ exchange information on the Web 12 | is modeled by the [REST architectural style](cite:citesAsAuthority REST). 13 | ``` 14 | will be processed into the following Markdown snippet: 15 | ```markdown 16 | The way _clients_ and _servers_ exchange information on the Web 17 | is modeled by the REST architectural style [1]. 18 | ``` 19 | 20 | This Markdown snippet 21 | ```markdown 22 | The way _clients_ and _servers_ exchange information on the Web 23 | is modeled by the REST architectural style [](cite:citesAsAuthority REST). 24 | ``` 25 | will be processed into the following Markdown snippet: 26 | ```markdown 27 | The way _clients_ and _servers_ exchange information on the Web 28 | is modeled by the REST architectural style [1]. 29 | ``` 30 | 31 | Furthermore, at the end of the Markdown document, 32 | a _References_ section will be added: 33 | ```html 34 |

References

35 |
36 |
[1]
37 |
Fielding, R.T., 2000. Architectural Styles and the Design of Network-based Software Architectures (PhD thesis). University of California.
38 |
39 | ``` 40 | 41 | ## Usage 42 | ```ruby 43 | require 'bibmarkdown' 44 | require 'bibtex' 45 | require 'csl/styles' 46 | 47 | content = <<-md 48 | The way _clients_ and _servers_ exchange information on the Web 49 | is modeled by the [REST architectural style](cite:citesAsAuthority REST). 50 | md 51 | 52 | bibliography = <<-bib 53 | @phdthesis{REST, 54 | author = {Roy Thomas Fielding}, 55 | title = {Architectural Styles and the Design of Network-based Software Architectures}, 56 | school = {University of California}, 57 | year = 2000, 58 | url = {http://www.ics.uci.edu/~fielding/pubs/dissertation/top.htm}, 59 | } 60 | bib 61 | 62 | entries = BibTeX.parse(bibliography).entries 63 | document = BibMarkdown::Document.new content, 64 | entries: entries, style: 'elsevier-harvard' 65 | puts document.to_markdown 66 | ``` 67 | 68 | ## Related work 69 | This library was inspired by [Pieter Colpaert](http://pieter.pm/)'s 70 | [jekyll-refs](https://github.com/pietercolpaert/jekyll-refs), 71 | which has some nice [CSS suggestions](https://github.com/pietercolpaert/jekyll-refs#using-css-styles-to-mark-up-the-citations) as well. 72 | 73 | ## License 74 | Copyright ©2016 Ruben Verborgh – MIT License 75 | -------------------------------------------------------------------------------- /lib/bibmarkdown/document.rb: -------------------------------------------------------------------------------- 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 | @references = {} 15 | 16 | # Replace all citations by links 17 | markdown = @source.gsub %r{\[([^\]]*)\]\(cit[eo]:(\w+)\s+([^\)]+)\)} do |match| 18 | text = $1; rel = $2; keys = $3 19 | 20 | # Create the references 21 | refs = keys.strip.split(/\s*,\s*/).map {|key| create_reference key } 22 | raise "Missing reference key in #{match}" if refs.empty? 23 | reflinks = create_element :span, "\\[" + (refs.map{|r| r[:link]}.join ", ") + "\\]", class: 'references' 24 | 25 | # If the anchor text is empty, output reference links 26 | if text.empty? 27 | reflinks 28 | # If the reference has no URL, output the anchor text and append reference links 29 | elsif refs.first[:url].empty? 30 | "#{text} #{reflinks}" 31 | # Otherwise, link the anchor text to the first reference URL and append reference links 32 | else 33 | property = 'schema:citation http://purl.org/spar/cito/' + rel 34 | # Link to the first reference 35 | first = refs.first 36 | if first[:id] == first[:url] 37 | "#{create_element :a, text, property: property, href: first[:url]} #{reflinks}" 38 | # If the reference's ID is different from its URL, surround with an extra span 39 | else 40 | link = create_element :a, text, href: first[:url] 41 | "#{create_element :span, link, property: property, resource: first[:id]} #{reflinks}" 42 | end 43 | end 44 | end 45 | 46 | # Append the reference list to the document 47 | "#{markdown}\n\n#{references_html}".rstrip 48 | end 49 | 50 | protected 51 | def find_entry key 52 | raise "Reference '#{key}' does not exist." unless @entries.key?(key) 53 | @entries[key] 54 | end 55 | 56 | def create_reference key 57 | return @references[key] if @references.has_key? key 58 | 59 | # Look up citation and its URL 60 | entry = find_entry key 61 | url = entry[:url] || '' 62 | 63 | # Assign a reference number and create a link to the reference 64 | number = @references.length + 1 65 | link = create_element :a, "#{number}", href: "#ref-#{number}" 66 | 67 | @references[key] = { id: reference_id(key), number: number, url: url, link: link } 68 | end 69 | 70 | def h text 71 | CGI::escapeHTML(text || '') 72 | end 73 | 74 | def create_element tag, html, attrs = {} 75 | attrs = attrs.map { |attr, value| %Q{#{attr}="#{h value}"} } 76 | %Q{<#{tag} #{attrs.join ' '}>#{html}} 77 | end 78 | 79 | def references_html 80 | if @references.empty? 81 | '' 82 | else 83 | html = %Q{

References

\n} 84 | html += %Q{
\n} 85 | @references.each do |key, ref| 86 | html += %Q{
\[#{ref[:number]}\]
\n} 87 | html += %Q{
#{reference_html key}
\n} 88 | end 89 | html += %Q{
\n} 90 | end 91 | end 92 | 93 | def reference_id key 94 | entry = find_entry key 95 | entry[:id] || 96 | entry[:doi] && "https://dx.doi.org/#{entry[:doi]}" || 97 | entry[:url] || 98 | "##{key}" 99 | end 100 | 101 | def reference_type key 102 | case find_entry(key).type 103 | when :article, :inproceedings 104 | 'schema:Article' 105 | when :book 106 | 'schema:Book' 107 | when :incollection 108 | 'schema:Chapter' 109 | when :mastersthesis, :phdthesis 110 | 'schema:Thesis' 111 | else 112 | 'schema:CreativeWork' 113 | end 114 | end 115 | 116 | def reference_html key 117 | # Prepare reference HTML renderer 118 | processor = CiteProc::Processor.new style: @style, format: 'html' 119 | citeproc = @entries[key].to_citeproc 120 | 121 | # Ensure multi-part last names stick together 122 | (citeproc["author"] || citeproc["editor"] || []).each do |author| 123 | if author.has_key? "non-dropping-particle" 124 | author["family"] = "#{author["non-dropping-particle"]} #{author["family"]}" 125 | author.delete "non-dropping-particle" 126 | end 127 | end 128 | 129 | # Render reference 130 | processor << citeproc 131 | citations = processor.render :bibliography, id: key 132 | citation = citations.first 133 | 134 | # Replace URLs by links 135 | citation.gsub %r{https?://[^ ]+[^ .]} do |match| 136 | create_element :a, h(match), href: match 137 | end 138 | end 139 | end 140 | end 141 | --------------------------------------------------------------------------------