hard_citer 0.0.3

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/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Michael Cordell
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Hard Citer
2
+ Hard Citer is a solution for outputting HTML bibliographies. When used in conjuction with a "cite while you write" tool, it can make writing and editing well-cited html eaiser. Using a in-text cited HTML document and a bibtex library, it can output a properly formatted bibliography. The default configuration is geared towards usage with Papers' [magic citations][1]. Papers does not provide an easy way to perform magic citations and produce a nicely formatted bibliography for HTML, from an HTML document source.
3
+
4
+ For the best bang for your buck, use one of the text editors listed [here][2]
5
+ under the heading "Insertation of citekey" so that you can easily cite while you
6
+ write. As an added benefit, papers will automatically group your citations in the
7
+ manuscript section. Export this group to a bibtex library and you are ready to
8
+ use Hard Citer.
9
+
10
+ [1]: http://support.mekentosj.com/kb/tutorials/magic-citations
11
+ [2]: http://support.mekentosj.com/kb/read-write-cite/applications-supported-by-magic-citations
12
+
13
+ ##Usage
14
+
15
+ Require the module where you plan on using HardCiter. Initialize a new Citer object by pointing it at the bib file you exported from Papers:
16
+
17
+ ```Ruby
18
+ require File.expand_path("../lib/hard_citer", __FILE__)
19
+ citer = HardCiter::Citer.new('./examples/example_bib.bib')
20
+ ```
21
+
22
+ Open the HTML file that contains the intext citations that match the bib file.
23
+
24
+ ```Ruby
25
+ input_html = File.open('./examples/example_input.html','r')
26
+ ```
27
+
28
+ Now, simply provide the input_html to the cite_text function and it will return the well-cited output text.
29
+
30
+ ```Ruby
31
+ html_output = citer.cite_text()
32
+ ```
33
+
34
+
35
+ You can change the output format of the citations by changing the CSL of the citer object before calling cite_text. Simply provide the path to the csl file that you would like to use.
36
+
37
+ ```Ruby
38
+ citer.csl = File.expand_path("../examples/plos.csl")
39
+ ```
40
+
41
+
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ :default => :spec
data/lib/hard_citer.rb ADDED
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+ require 'bibtex'
3
+ require 'citeproc'
4
+ require File.expand_path("../hardciter/bibliography.rb", __FILE__)
5
+ require File.expand_path("../hardciter/cite_match.rb", __FILE__)
6
+ require File.expand_path("../hardciter/citer.rb", __FILE__)
7
+ require File.expand_path("../hardciter/configuration.rb", __FILE__)
8
+ require File.expand_path("../hardciter/document.rb", __FILE__)
9
+ require File.expand_path("../hardciter/exceptions.rb", __FILE__)
10
+ require File.expand_path("../hardciter/library.rb", __FILE__)
11
+ require File.expand_path("../hardciter/styler.rb", __FILE__)
12
+
13
+
14
+
15
+
16
+
@@ -0,0 +1,85 @@
1
+ module HardCiter
2
+ class Bibliography
3
+ attr_accessor :bibliography_intext, :intext_regex, :bib_out_location, :citation_locations, :citations
4
+
5
+ def initialize
6
+ @bibliography_intext = HardCiter::Configuration::BIBLIOGRAPHY_INTEXT_KEY
7
+ @intext_regex = HardCiter::Configuration::INTEXT_REGEX
8
+ @citation_locations = {}
9
+ @citations = {}
10
+ end
11
+
12
+ def find_intext_citations(line)
13
+ line.enum_for(:scan, @intext_regex).map do
14
+ match = Regexp.last_match
15
+ pos = match.begin(0)
16
+ [match.to_s, pos]
17
+ end
18
+ end
19
+
20
+ def has_bibliography_key?(line)
21
+ line =~ @bibliography_intext
22
+ end
23
+
24
+ def mark_match_positions(citation_matches,line,index)
25
+ if citation_matches.length > 1
26
+ @citation_locations[index] = group_matches(citation_matches, line)
27
+ else
28
+ @citation_locations[index] = citation_matches
29
+ end
30
+ end
31
+
32
+ def parse_line(line,index)
33
+ @bib_out_location = index if has_bibliography_key?(line)
34
+ regex_matches = find_intext_citations(line)
35
+ end
36
+
37
+ def group_matches(matches,line)
38
+ matches_grouped = []
39
+ current_match = matches.shift
40
+ until matches.empty?
41
+ next_match = matches.shift
42
+ if matches_are_paired?(current_match,next_match,line)
43
+ current_match = pair_two_matches(current_match, next_match)
44
+ else
45
+ matches_grouped.push(current_match) if current_match
46
+ current_match = next_match
47
+ end
48
+ end
49
+ matches_grouped.push(current_match) unless current_match.empty?
50
+ return matches_grouped
51
+ end
52
+
53
+ def pair_two_matches(current_match,next_match)
54
+ if current_match[0].instance_of?(Array)
55
+ paired_matches = current_match + next_match
56
+ else
57
+ paired_matches = [current_match,next_match]
58
+ end
59
+ end
60
+
61
+ def matches_are_paired?(match_first, match_second, line)
62
+ match_text, match_pos = get_match_text_and_pos(match_first)
63
+ next_match_pos = match_second[1]
64
+ end_of_match = match_pos + match_text.key.length
65
+ while end_of_match < next_match_pos
66
+ if line[end_of_match] =~ /\s/
67
+ end_of_match += 1
68
+ else
69
+ false
70
+ end
71
+ end
72
+ true
73
+ end
74
+
75
+ def get_match_text_and_pos(match_group)
76
+ if match_group[0].instance_of?(Array)
77
+ #match_group has sub arrays so it needs to be unpacked
78
+ last_match = match_group[match_group.size-1]
79
+ else
80
+ last_match = match_group
81
+ end
82
+ [last_match[0],last_match[1]]
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+ module HardCiter
3
+ class CiteMatch
4
+ attr_accessor :bib_number, :citation, :in_cite_text, :key
5
+
6
+ def initialize(key, citation = nil, in_cite_text = nil)
7
+ @key = key
8
+ @citation = citation
9
+ @in_cite_text = in_cite_text
10
+ end
11
+
12
+ end
13
+ end
14
+
@@ -0,0 +1,94 @@
1
+ module HardCiter
2
+ class Citer
3
+ BIBTEX_LIBRARY_REGEX = /\.bib$/
4
+ attr_accessor :library, :styler, :csl, :bibliography
5
+
6
+ #Citer
7
+ def initialize(path = nil)
8
+ initialize_library_by_path(path) if path
9
+ @bibliography = HardCiter::Bibliography.new
10
+ @styler = HardCiter::Styler.new
11
+ @csl = HardCiter::Configuration::CSL
12
+ end
13
+
14
+ #Citer
15
+ def initialize_library_by_path(path)
16
+ if path =~ BIBTEX_LIBRARY_REGEX
17
+ @library = BibTexLibrary.new(path)
18
+ else
19
+ raise "Unknown path type"
20
+ end
21
+ end
22
+
23
+ #Citer
24
+ def cite_text(text)
25
+ #validate_prerequisites
26
+ doc = Document.new(text)
27
+ find_all_citations(doc.text_array) #move this from bib to doc? TODO
28
+ output_text = integrate_citations_into_text(doc.text_array)
29
+ output_text = integrate_bibliography_into_text(output_text)
30
+ end
31
+
32
+ def validate_prerequisites
33
+ #TODO
34
+ #check to make sure we have everything we need
35
+ end
36
+
37
+ #Citer
38
+ #refactor to use Document
39
+ def find_all_citations(text)
40
+ text.each_with_index() do |line, index|
41
+ regex_matches = @bibliography.parse_line(line,index)
42
+ matches = regex_matches_to_cite_matches(regex_matches) unless regex_matches.empty?
43
+ @bibliography.mark_match_positions(matches,line,index) unless matches.nil?
44
+ end
45
+ end
46
+
47
+ def regex_matches_to_cite_matches(regex_matches)
48
+ regex_matches.map { |m| get_or_create_cite_match(m) }
49
+ end
50
+
51
+ # Document, Bib
52
+ def integrate_citations_into_text(text)
53
+ output_text = text.dup
54
+ @bibliography.citation_locations.each do |line_number,cite_matches|
55
+ text_line = output_text[line_number]
56
+ output_text[line_number] = @styler.style_line(text_line,cite_matches)
57
+ end
58
+ output_text
59
+ end
60
+
61
+ #
62
+ def integrate_bibliography_into_text(text)
63
+ out_location = @bibliography.bib_out_location
64
+ if out_location
65
+ bib_text = styler.get_bibliography_lines(@bibliography, @csl)
66
+ bib_end = out_location + 1
67
+ after_bib_location = text[(bib_end..text.size-1)]
68
+ return text[0..bib_end] + bib_text + after_bib_location
69
+ else
70
+ warn('No bib key found. \
71
+ Bibliography not added to cited text.')
72
+ return text
73
+ end
74
+ end
75
+
76
+ def get_or_create_cite_match(regex_match)
77
+ match_key, match_pos = regex_match
78
+
79
+ if @bibliography.citations.has_key?(match_key)
80
+ cite_match = @bibliography.citations[match_key]
81
+ else
82
+ cite_match = CiteMatch.new(match_key)
83
+ cite_match.bib_number = @bibliography.citations.length + 1
84
+ cite_match.citation = @library.get_citation(convert_match_to_cite_key(match_key))
85
+ @bibliography.citations[match_key] = cite_match
86
+ end
87
+ [cite_match, match_pos]
88
+ end
89
+
90
+ def convert_match_to_cite_key(match)
91
+ match.gsub(/\{|\}/, '')
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,34 @@
1
+ module HardCiter
2
+ class Configuration
3
+ attr_accessor :csl, :bibliography_intext, :intext_regex
4
+
5
+ CSL = File.expand_path("../../../examples/plos.csl", __FILE__)
6
+
7
+ BIBLIOGRAPHY_INTEXT_KEY = /\{papers2_bibliography\}/
8
+
9
+ INTEXT_REGEX = /\{(\w*:\w*)\}/
10
+
11
+ def initialize
12
+ self.reset
13
+ end
14
+
15
+ def reset
16
+ @csl = CSL
17
+ @bibliography_intext = BIBLIOGRAPHY_INTEXT_KEY
18
+ @intext_regex = INTEXT_REGEX
19
+ end
20
+
21
+ end
22
+
23
+ class << self
24
+ attr_accessor :configuration
25
+
26
+ def configuration
27
+ @configuration ||= Configuration.new
28
+ end
29
+ end
30
+
31
+ def self.configure
32
+ yield configuration if block_given?
33
+ end
34
+ end
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+ require 'stringio'
3
+ require File.expand_path('../exceptions', __FILE__)
4
+
5
+ module HardCiter
6
+ class Document
7
+ attr_reader :text_array
8
+
9
+ def initialize(text = nil)
10
+ self.text_array = text
11
+ end
12
+
13
+ def text_array=(text)
14
+ if text.is_a? File
15
+ @text_array = text.readlines()
16
+ elsif text.is_a? String
17
+ @text_array = StringIO.open(text).readlines()
18
+ elsif text.is_a? Array
19
+ @text_array = text
20
+ elsif text.nil?
21
+ @text_array = nil
22
+ else
23
+ raise HardCiter::InvalidTextFormat
24
+ end
25
+ end
26
+
27
+ end
28
+ end
29
+
30
+
31
+
@@ -0,0 +1,3 @@
1
+ module HardCiter
2
+ class InvalidTextFormat < StandardError; end
3
+ end
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+ require 'bibtex'
3
+ module HardCiter
4
+ class Library < Hash
5
+
6
+ def initialize(path = nil)
7
+ load_lib(path)
8
+ end
9
+
10
+
11
+ def load_lib(path = nil)
12
+ if File.exists? path
13
+ load_from_file(path)
14
+ end
15
+ end
16
+
17
+ def get_citation()
18
+ end
19
+ end
20
+
21
+
22
+ class BibTexLibrary
23
+ attr_accessor :bibtex
24
+
25
+ def initialize(path)
26
+ load_from_file(path)
27
+ end
28
+
29
+ def load_from_file(path)
30
+ @bibtex = BibTeX.open(path)
31
+ end
32
+
33
+ def get_citation(key)
34
+ @bibtex[key]
35
+ end
36
+
37
+ def method_missing(method, *args, &block)
38
+ @bibtex.send method, *args, &block
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,81 @@
1
+ # encoding: utf-8
2
+ require 'citeproc'
3
+ module HardCiter
4
+ class Styler
5
+
6
+ def initialize
7
+ @open_tag = ''
8
+ @close_tag = ''
9
+ @multi_separator = '<sup>, </sup>'
10
+ @separator_after_last = false
11
+ end
12
+
13
+ def get_bibliography_lines(bibliography_array, csl_style)
14
+ out_lines = ['<ol class="bibliography">']
15
+ bibliography_array.citations.each do |cite_key, cite_match|
16
+ entry = cite_match.citation
17
+ if entry.nil?
18
+ cite_text = cite_key
19
+ else
20
+ cite_text = CiteProc.process(cite_match.citation.to_citeproc,
21
+ style: csl_style, format: :html)
22
+ strip_extra_papers_brackets cite_text
23
+ end
24
+
25
+ out_lines.push '<li><a name = "' +
26
+ "bibliography_#{cite_match.bib_number}\">" +
27
+ cite_text + '</a></li>'
28
+ end
29
+ out_lines.push '</ol>'
30
+ end
31
+
32
+ def strip_extra_papers_brackets(line)
33
+ line.gsub!(/\{|\}/, '')
34
+ end
35
+
36
+ def style_line(line, citations)
37
+ processed_line = ''
38
+ pos_off_set = 0
39
+ citations.each do |cite_match|
40
+ if cite_match[0].is_a?(CiteMatch)
41
+ output, offset = single_cite(cite_match[0],
42
+ cite_match[1] - pos_off_set, line)
43
+ pos_off_set += offset
44
+
45
+ elsif cite_match[0].is_a? Array
46
+ output = multi_cite(cite_match, line, pos_off_set)
47
+ end
48
+ if output.nil?
49
+ puts "wait"
50
+ end
51
+ processed_line += output
52
+ end
53
+ processed_line += line
54
+ end
55
+
56
+ def multi_cite(cite_match_array, line, pos_offset)
57
+ output_line = @open_tag
58
+ cite_match_array.each_with_index do |cite_match, index|
59
+ citation, pos = cite_match
60
+ output, off_set = single_cite(citation, pos - pos_offset, line)
61
+ output_line += output
62
+ output_line += @multi_separator unless index == cite_match.size - 1
63
+ pos_offset += off_set
64
+ end
65
+ output_line += @close_tag
66
+
67
+ end
68
+
69
+ def single_cite(citation, pos, line)
70
+ key = citation.key
71
+ pos == 0 ? before_cite = '' : before_cite = line.slice!(0..pos - 1)
72
+ cite = line.slice!(0..key.length - 1)
73
+ off_set = before_cite.length + cite.length
74
+ in_text_citation = "<sup><a href = \"#bibliography_#{citation.bib_number}\
75
+ \">#{citation.bib_number}</a></sup>"
76
+ output_line = before_cite + in_text_citation
77
+ [output_line, off_set]
78
+ end
79
+
80
+ end
81
+ end
data/lib/scrap_methods ADDED
@@ -0,0 +1,44 @@
1
+ def find_blocks(lines)
2
+ r=Regexp.compile('\{.*\}')
3
+ line_count=0
4
+ lines.each do
5
+ |line|
6
+ matches=r.match(all_lines)
7
+ matches.each { |match| puts "Line # #{line_count}: #{match}" }
8
+ line_count += 1
9
+ end
10
+ end
11
+
12
+ def find_blocks_manually(lines)
13
+ line_count=0
14
+ pairs=[]
15
+ open_blocks = []
16
+ lines.each do
17
+ |line|
18
+ char_line_count=0
19
+ line.each_char do
20
+ |c|
21
+ if c.eql?("{")
22
+ open_blocks.push([line_count,char_line_count])
23
+ elsif c.eql?("}") && open_blocks.empty?
24
+ puts "Un-openend close found at Line Num = #{line_count}, "+
25
+ "Char_num= #{char_line_count}"
26
+ elsif c.eql?("}") && open_blocks.any?
27
+ opening=open_blocks.pop
28
+ puts "Block at opening: LN=#{opening[0]} CN=#{opening[1]}"
29
+ puts "Block closed at: LN=#{line_count} CN=#{char_line_count}"
30
+ end
31
+ char_line_count+=1
32
+ end
33
+ line_count+=1
34
+ end
35
+ end
36
+
37
+ def find_citations (text)
38
+ if text.is_a?(String)
39
+ text=text
40
+ elsif text_is_a?(Array)
41
+ text=text.join
42
+ end
43
+ end
44
+
metadata ADDED
@@ -0,0 +1,63 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hard_citer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael Cordell
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-18 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! "\nHard Citer is a solution for outputting HTML bibliographies. When
15
+ used\nin conjuction with a \"cite while you write\" tool, it can make writing and\nediting
16
+ well-cited html eaiser. This may be particularly useful for \nacademics publishing
17
+ online. Using a in-text cited HTML document and\na bibtex library, it can output
18
+ a properly formatted bibliography.\n"
19
+ email:
20
+ - surpher@gmail.com
21
+ executables: []
22
+ extensions: []
23
+ extra_rdoc_files: []
24
+ files:
25
+ - README.md
26
+ - Rakefile
27
+ - LICENSE.txt
28
+ - lib/hard_citer.rb
29
+ - lib/hardciter/bibliography.rb
30
+ - lib/hardciter/cite_match.rb
31
+ - lib/hardciter/citer.rb
32
+ - lib/hardciter/configuration.rb
33
+ - lib/hardciter/document.rb
34
+ - lib/hardciter/exceptions.rb
35
+ - lib/hardciter/library.rb
36
+ - lib/hardciter/styler.rb
37
+ - lib/scrap_methods
38
+ homepage: https://github.com/mcordell/hard_citer
39
+ licenses: []
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 1.8.25
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: A gem to help with in-text citations in HTML documents.
62
+ test_files: []
63
+ has_rdoc: