texstylist 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +50 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +83 -0
- data/Rakefile +10 -0
- data/example/example_stylize.rb +33 -0
- data/lib/texstylist.rb +107 -0
- data/lib/texstylist/citations.rb +112 -0
- data/lib/texstylist/csl_adaptor.rb +147 -0
- data/lib/texstylist/csl_constants.rb +18 -0
- data/lib/texstylist/latex_util.rb +51 -0
- data/lib/texstylist/unicode_babel.rb +350 -0
- data/texstylist.gemspec +32 -0
- metadata +227 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6299c4ebad14379ad4b1f5a5d818e108bd67e6a3
|
4
|
+
data.tar.gz: d04ff2b8a292d9da9857e8f85ccae402e91f21fe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 21b778bacd507125712d8c0de512534ee9202b5a24335312af26e1fc395b48ab4f8b303c8ce6fc53909083521d9144c1830585c3d051cd6ff801611341f67962
|
7
|
+
data.tar.gz: d23c43014c90b60febeea918a90f86c0361c97f8a14ed85241253d33afc48eed19f3e668c2f6580ca2cb1edefa6a11b83491282c0e28c0c0a805d5191015cd7d
|
data/.gitignore
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
# Used by dotenv library to load environment variables.
|
14
|
+
# .env
|
15
|
+
|
16
|
+
## Specific to RubyMotion:
|
17
|
+
.dat*
|
18
|
+
.repl_history
|
19
|
+
build/
|
20
|
+
*.bridgesupport
|
21
|
+
build-iPhoneOS/
|
22
|
+
build-iPhoneSimulator/
|
23
|
+
|
24
|
+
## Specific to RubyMotion (use of CocoaPods):
|
25
|
+
#
|
26
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
27
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
28
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
29
|
+
#
|
30
|
+
# vendor/Pods/
|
31
|
+
|
32
|
+
## Documentation cache and generated files:
|
33
|
+
/.yardoc/
|
34
|
+
/_yardoc/
|
35
|
+
/doc/
|
36
|
+
/rdoc/
|
37
|
+
|
38
|
+
## Environment normalization:
|
39
|
+
/.bundle/
|
40
|
+
/vendor/bundle
|
41
|
+
/lib/bundler/man/
|
42
|
+
|
43
|
+
# for a library or gem, you might want to ignore these files since the code is
|
44
|
+
# intended to run in multiple environments; otherwise, check them in:
|
45
|
+
Gemfile.lock
|
46
|
+
.ruby-version
|
47
|
+
.ruby-gemset
|
48
|
+
|
49
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
50
|
+
.rvmrc
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Deyan Ginev
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
# TeX Stylist
|
2
|
+
|
3
|
+
[Authorea](http://www.authorea.com)'s TeX-based stylist. Think Instagram filters for scholarly documents.
|
4
|
+
|
5
|
+
**CAUTION: This repository is in a pre-alpha dev sprint, consider it completely unstable until a 0.1.0 release**
|
6
|
+
|
7
|
+
[](https://travis-ci.org/Authorea/texstylist)
|
8
|
+
[](https://raw.githubusercontent.com/authorea/texstylist/master/LICENSE)
|
9
|
+
[](https://badge.fury.io/rb/texstylist)
|
10
|
+
|
11
|
+
## Common Questions
|
12
|
+
|
13
|
+
**Who is this gem intended for?** Mostly for people working on micro-publication platforms interested in a turnkey solution to customizing the appearance of exported documents. If you're an author you can simply, and freely, use the export features of [Authorea](https://www.authorea.com).
|
14
|
+
|
15
|
+
**Can I directly use it on my LaTeX documents?** Almost. As convention has it with Authorea, you can use your document body directly, but we request that you prepare the document metadata separately, together with the customization parameters.
|
16
|
+
|
17
|
+
We have also released the [texstyles](https://github.com/Authorea/texstyles) Ruby gem, which contains the full list of scholarly styles used at Authorea. We welcome contributions and corrections!
|
18
|
+
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
require 'texstylist'
|
24
|
+
|
25
|
+
header = '% A latex preamble, of e.g. custom macro definitions, or custom overrides for the desired style'
|
26
|
+
abstract = 'An (optional) document abstract'
|
27
|
+
body = 'An example article body.'
|
28
|
+
|
29
|
+
metadata = {
|
30
|
+
'title' => 'An example scholarly article',
|
31
|
+
'abstract' => abstract,
|
32
|
+
# ... full range of scholarly metadata omitted for space
|
33
|
+
'bibliography' => 'biblio.bib',
|
34
|
+
# any bibtex or CSL citation style is accepted
|
35
|
+
'citation_style' => 'apacite',
|
36
|
+
}
|
37
|
+
|
38
|
+
# Any available Style from the texstyles gem is accepted
|
39
|
+
stylist = Texstylist.new(:authorea)
|
40
|
+
# A single render call styles the document and citations, typesets the metadata, and handles internationalization
|
41
|
+
styled_doc = stylist.render(body, header, metadata)
|
42
|
+
|
43
|
+
# Enjoy!
|
44
|
+
```
|
45
|
+
|
46
|
+
You can see a full example [here](https://github.com/Authorea/texstylist/blob/master/example/example_stylize.rb).
|
47
|
+
|
48
|
+
## Installation
|
49
|
+
|
50
|
+
Add this line to your application's Gemfile:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
gem 'texstylist'
|
54
|
+
```
|
55
|
+
|
56
|
+
And then execute:
|
57
|
+
|
58
|
+
$ bundle
|
59
|
+
|
60
|
+
Or install it yourself as:
|
61
|
+
|
62
|
+
$ gem install texstylist
|
63
|
+
|
64
|
+
## Roadmap
|
65
|
+
|
66
|
+
### Supported via [Texstyles](https://github.com/Authorea/texstyles)
|
67
|
+
* 100+ and growing scholarly export styles
|
68
|
+
* Core metadata items of scholarly articles
|
69
|
+
* White/blacklisting LaTeX style and class conflicts
|
70
|
+
* Independent citation style specifications
|
71
|
+
|
72
|
+
### Support via [Texstylist](https://github.com/Authorea/texstylist)
|
73
|
+
* Unicode-only input and output
|
74
|
+
* Automatic internationalization for LaTeX via babel and pdflatex, by analyzing Unicode locales
|
75
|
+
* Citation styling API, supporting both [CSL](http://citationstyles.org/) and [bibtex](http://www.bibtex.org/) style files (.bst)
|
76
|
+
|
77
|
+
### Upcoming
|
78
|
+
* Use a standard vocabulary and serialization format(s) for scholarly metadata
|
79
|
+
* Undergo a round of community feedback and evolve the gem respectively
|
80
|
+
|
81
|
+
## License
|
82
|
+
|
83
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'texstylist'
|
2
|
+
|
3
|
+
header = '% A latex preamble, of e.g. custom macro definitions, or custom overrides for the desired style'
|
4
|
+
abstract = 'An (optional) document abstract'
|
5
|
+
body = 'An example article body.'
|
6
|
+
|
7
|
+
metadata = {
|
8
|
+
'title' => 'An example scholarly article',
|
9
|
+
'short_title' => 'Example article',
|
10
|
+
'authors' => [
|
11
|
+
{ 'name' => 'First Author',
|
12
|
+
'affiliation' => 1},
|
13
|
+
{ 'name' => 'Second Author',
|
14
|
+
'affiliation' => 2},
|
15
|
+
{ 'name' => 'Third Author',
|
16
|
+
'affiliations' => [1, 2]}
|
17
|
+
],
|
18
|
+
'affiliations' => {
|
19
|
+
1 => 'Example Organization',
|
20
|
+
2 => 'Another Organization'
|
21
|
+
},
|
22
|
+
'abstract' => abstract
|
23
|
+
}
|
24
|
+
|
25
|
+
|
26
|
+
# Choose any available Texstyles::Style here
|
27
|
+
stylist = Texstylist.new(:authorea)
|
28
|
+
|
29
|
+
# A single render call styles the document and citations, typesets the metadata, and handles internationalization
|
30
|
+
stylized_document = stylist.render(body, header, metadata)
|
31
|
+
|
32
|
+
# Enjoy!
|
33
|
+
puts stylized_document
|
data/lib/texstylist.rb
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'texstyles'
|
2
|
+
require 'texstylist/citations'
|
3
|
+
require 'texstylist/unicode_babel'
|
4
|
+
|
5
|
+
class Texstylist
|
6
|
+
attr_accessor :style
|
7
|
+
@@default_package_selection = %w(
|
8
|
+
graphicx grffile latexsym textcomp longtable multirow booktabs ams natbib url hyperref latexml
|
9
|
+
inputenc babel)
|
10
|
+
@@default_package_options = {'grffile' => ['space'], 'inputenc' => ['utf8']}
|
11
|
+
|
12
|
+
def initialize(style = :authorea, package_candidates = @@default_package_selection)
|
13
|
+
@style = Texstyles::Style.new(style)
|
14
|
+
# setup default packages
|
15
|
+
@default_packages_list = package_candidates.select{|candidate| @style.package_compatible?(candidate)}
|
16
|
+
end
|
17
|
+
|
18
|
+
def render(body, header=nil, metadata = {})
|
19
|
+
return '' if body.empty?
|
20
|
+
@header = header
|
21
|
+
|
22
|
+
# I. Prepare default package inclusions
|
23
|
+
@default_packages = ''
|
24
|
+
@default_packages_list.each do |package|
|
25
|
+
next if @header && @header.match(/\{(?:#{package})\}/) # skip if overridden by the header.
|
26
|
+
options = @@default_package_options[package]
|
27
|
+
setup_macro = nil
|
28
|
+
|
29
|
+
# I.1. Expand common aliases, prepare extra setup steps
|
30
|
+
case package
|
31
|
+
when 'ams' # alias for a family of packages
|
32
|
+
package = 'amsfonts,amsmath,amssymb'
|
33
|
+
when 'hyperref'
|
34
|
+
setup_macro = "\\hypersetup{colorlinks=false,pdfborder={0 0 0}}"
|
35
|
+
when 'latexml'
|
36
|
+
package = nil
|
37
|
+
setup_macro = "\% You can conditionalize code for latexml or normal latex using this.\n"+
|
38
|
+
"\\newif\\iflatexml\\latexmlfalse"
|
39
|
+
when 'babel'
|
40
|
+
# handle globally, as we need to automagically internationalize any Unicode
|
41
|
+
package = nil
|
42
|
+
end
|
43
|
+
|
44
|
+
# I.2. Add the package inclusion, if any
|
45
|
+
if package
|
46
|
+
@default_packages << if options
|
47
|
+
"\\usepackage[#{options.join(',')}]{#{package}}"
|
48
|
+
else
|
49
|
+
"\\usepackage{#{package}}"
|
50
|
+
end
|
51
|
+
@default_packages << "\n"
|
52
|
+
end
|
53
|
+
# I.3 Add the setup macro, if any
|
54
|
+
if setup_macro
|
55
|
+
@default_packages << setup_macro + "\n"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# II. Special graceful degradation treatment for common sources of conflicts, done once globally
|
60
|
+
if !@style.package_compatible?(:natbib)
|
61
|
+
@default_packages << "\n\\newcommand\\citet{\\cite}\n\\newcommand\\citep{\\cite}"
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
# III. Advanced auto-magical internationalization of unicode with babel (intended for use with pdflatex)
|
66
|
+
if @style.package_compatible?(:babel)
|
67
|
+
# Having the full body and preamble, figure out which flavours of babel we need (and potentially other text-dependent logic)
|
68
|
+
metadata["default_packages"] = @default_packages
|
69
|
+
metadata["header"] = @header
|
70
|
+
preamble = @style.stylize_metadata(metadata)
|
71
|
+
# We'll have to rerender the preamble with all language locales setup
|
72
|
+
@default_packages << UnicodeBabel::latex_inclusions(preamble + body)
|
73
|
+
@default_packages << "\n"
|
74
|
+
# And auto-deposit various language activation macros in the article itself
|
75
|
+
body = UnicodeBabel::activate_foreign_languages(body)
|
76
|
+
end
|
77
|
+
|
78
|
+
# IV. Render the preamble and prepare the final latex document
|
79
|
+
metadata["default_packages"] = @default_packages
|
80
|
+
metadata["header"] = @header
|
81
|
+
preamble = @style.stylize_metadata(metadata)
|
82
|
+
article = preamble + "\n\n" + body
|
83
|
+
|
84
|
+
# IV.1. Normalize to simpler latex
|
85
|
+
article = self.simplify_latex(article)
|
86
|
+
|
87
|
+
# IV.2 Perform citations styling
|
88
|
+
article = self.stylize_citations(article, metadata)
|
89
|
+
# IV.3. Wrap up
|
90
|
+
article << "\n\\end{document}" if @style.package_compatible?(:latex) # finalize latex documents
|
91
|
+
article << "\n\n"
|
92
|
+
|
93
|
+
return article
|
94
|
+
end
|
95
|
+
|
96
|
+
def simplify_latex(text)
|
97
|
+
# \amp can be written as simply \&
|
98
|
+
text = text.gsub(/\\amp([^\w])/, "\\\\&\\1")
|
99
|
+
# simplify new line markup if needed
|
100
|
+
text = text.gsub(/\r\n/, "\n")
|
101
|
+
end
|
102
|
+
|
103
|
+
def stylize_citations(article, metadata)
|
104
|
+
Citations::stylize_citations(article, metadata['bibliography'], @style, metadata['citation_style'], decorate: metadata['decorate_citations'])
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'texstylist/csl_adaptor'
|
2
|
+
class Citations
|
3
|
+
|
4
|
+
# Taken from our Authorea TeX server - texlive 2015 and custom journal styles.
|
5
|
+
@@citation_styles_bst = Set.new(["ACM-Reference-Format-Journals", "AISB2008", "CUEDbiblio", "ChemCommun", "ChemEurJ",
|
6
|
+
"Chicago", "IEEEtran", "IEEEtranM", "IEEEtranMN", "IEEEtranN", "IEEEtranS", "IEEEtranSA", "IEEEtranSN", "InorgChem",
|
7
|
+
"JAmChemSoc", "JAmChemSoc_all", "JHEP", "LHCb", "PhDbiblio-bold", "PhDbiblio-case", "PhDbiblio-url", "PhDbiblio-
|
8
|
+
url2", "Science", "ScienceAdvances", "UNAMThesis", "aa", "aaai", "aaai-named", "aabbrv", "aalpha", "abbrv", "abbrv-
|
9
|
+
fr", "abbrv-letters", "abbrvcnb", "abbrvdin", "abbrvhtml", "abbrvnat", "abbrvnat-fr", "abbrvurl", "abntex2-alf",
|
10
|
+
"abntex2-num", "abstract", "achemso", "achicago", "acl", "acm", "acm-fa", "acm-sigchi", "acmtrans-ims", "address",
|
11
|
+
"address-html", "address-ldif", "address-vcard", "adfathesis", "adrbirthday", "adrconv", "adrfax", "aer", "aertt",
|
12
|
+
"agecon", "agsm", "agu", "agu04", "agu08", "agufull", "agufull04", "agufull08", "aiaa", "aichej", "aipauth4-1",
|
13
|
+
"aipnum4-1", "aj", "ajae", "ajl", "alpha", "alpha-fr", "alpha-letters", "alphadin", "alphahtml", "alphahtmldate",
|
14
|
+
"alphahtmldater", "alphaurl", "ametsoc", "ametsoc2014", "ams-alph", "ams-pln", "amsalpha", "amsplain", "amsra",
|
15
|
+
"amsrn", "amsrs", "amsru", "amsry", "angew", "annotate", "annotation", "anotit", "aomalpha", "aomplain", "apa",
|
16
|
+
"apacann", "apacannx", "apacite", "apacitex", "apalike", "apalike-fr", "apalike-letters", "apalike2", "apanat1b",
|
17
|
+
"apecon", "apj", "aplain", "apsr", "apsrev", "apsrev4-1", "apsrevM", "apsrmp", "apsrmp4-1", "apsrmpM", "asa-fa",
|
18
|
+
"asaetr", "ascelike", "asp2010", "astron", "atlasBibStyleWithTitle", "atlasBibStyleWoTitle", "aunsnot", "aunsrt",
|
19
|
+
"authordate1", "authordate2", "authordate3", "authordate4", "bababbr3", "bababbr3-fl", "bababbr3-lf", "bababbrv",
|
20
|
+
"bababbrv-fl", "bababbrv-lf", "babalpha", "babalpha-fl", "babalpha-lf", "babamspl", "babplai3", "babplai3-fl",
|
21
|
+
"babplai3-lf", "babplain", "babplain-fl", "babplain-lf", "babunsrt", "babunsrt-fl", "babunsrt-lf", "bbs",
|
22
|
+
"besjournals", "bestpapers", "bestpapers-export", "bgteuabbr", "bgteuabbr2", "bgteupln", "bgteupln2", "bgteupln3",
|
23
|
+
"biblatex", "bibtoref", "biochem", "birthday", "bmc-mathphys", "bookdb", "cascadilla", "cbe", "cc", "cc2", "cell",
|
24
|
+
"chetref", "chicago", "chicago-annote", "chicago-fa", "chicagoa", "chronological", "chronoplainnm", "chscite",
|
25
|
+
"cje", "cmpj", "cont-ab", "cont-au", "cont-no", "cont-ti", "copernicus", "cv", "databib", "dcbib", "dcu", "dinat",
|
26
|
+
"dk-abbrv", "dk-alpha", "dk-apali", "dk-plain", "dk-unsrt", "dlfltxbbibtex", "dtk", "easy", "ecca", "ecta",
|
27
|
+
"elsarticle-harv", "elsarticle-num", "elsarticle-num-names", "email", "email-html", "en-mtc", "erae", "expcites",
|
28
|
+
"expkeys", "export", "fbs", "fcavtex", "figbib", "figbib1", "finplain", "fr-mtc", "francais", "francaissc",
|
29
|
+
"frontiersinMED", "frontiersinMED&FPHY", "frontiersinSCNS&ENG", "frplainnat-letters", "gatech-thesis", "gatech-
|
30
|
+
thesis-losa", "genetics", "gerabbrv", "geralpha", "gerapali", "gerplain", "gerunsrt", "gji", "glsplain", "glsshort",
|
31
|
+
"gost2003", "gost2003s", "gost2008", "gost2008l", "gost2008ls", "gost2008n", "gost2008ns", "gost2008s", "gost705",
|
32
|
+
"gost705s", "gost780", "gost780s", "h-physrev", "hc-de", "hc-en", "humanbio", "humannat", "iclr2015", "ieeepes",
|
33
|
+
"ieeetr", "ieeetr-fa", "ieeetr-fr", "ier", "ifacconf", "ifacconf-harvard", "ijmart", "ijqc", "imac", "imsart-
|
34
|
+
nameyear", "imsart-number", "inlinebib", "iopart-num", "is-abbrv", "is-alpha", "is-plain", "is-unsrt", "itaxpf",
|
35
|
+
"iucr", "jabbrv", "jae", "jalpha", "jas99", "jbact", "jcc", "jfm", "jipsj", "jmb", "jmr", "jname", "jneurosci",
|
36
|
+
"jorsj", "jox", "jpc", "jpe", "jphysicsB", "jplain", "jponew", "jss2", "jtb", "jthcarsu", "junsrt", "jurabib",
|
37
|
+
"jurarsp", "jureco", "jurunsrt", "jxb", "klunamed", "klunum", "kluwer", "ksfh_nat", "letter", "listbib", "ltugbib",
|
38
|
+
"mbplain", "mbunsrtdin", "mdpi", "mn2e", "mnras", "mslapa", "munich", "mybibstyle", "named", "namunsrt", "nar",
|
39
|
+
"natbib", "natdin", "naturemag", "nddiss2e", "nederlands", "newapa", "newapave", "oega", "ol", "opcit", "osajnl",
|
40
|
+
"papalike", "pccp", "perception", "phaip", "phapalik", "phcpc", "phiaea", "phjcp", "phnf", "phnflet", "phone",
|
41
|
+
"phpf", "phppcf", "phreport", "phrmp", "plabbrv", "plain", "plain-fa", "plain-fa-inLTR", "plain-fa-inLTR-beamer",
|
42
|
+
"plain-fr", "plain-letters", "plainDemo", "plaindin", "plainhtml", "plainhtmldate", "plainhtmldater", "plainnat",
|
43
|
+
"plainnat-fa", "plainnat-fr", "plainnat-letters", "plainnm", "plainurl", "plainyr", "plalpha", "plos2009",
|
44
|
+
"plos2015", "plplain", "plunsrt", "pnas", "pnas2009", "psuthesis", "refer", "regstud", "resphilosophica",
|
45
|
+
"revcompchem", "rsc", "rusnat", "sageep", "sapthesis", "savetrees", "seg", "seuthesis", "siam", "siam-fr", "siam-
|
46
|
+
letters", "spbasic", "spiebib", "spiejour", "splncs03", "spmpsci", "spphys", "sweabbrv", "swealpha", "sweplain",
|
47
|
+
"sweplnat", "sweunsrt", "tandfx", "tex-live", "texsis", "thesnumb", "thubib", "tieice", "tipsj", "trb", "tufte",
|
48
|
+
"udesoftec", "uestcthesis", "ugost2003", "ugost2003s", "ugost2008", "ugost2008l", "ugost2008ls", "ugost2008n",
|
49
|
+
"ugost2008ns", "ugost2008s", "unified", "unsrt", "unsrt-fa", "unsrt-fr", "unsrtabbrv3", "unsrtdin", "unsrthtml",
|
50
|
+
"unsrtnat", "unsrtnat-fr", "unsrtnm", "unsrturl", "upmplainnat", "usmeg-a", "usmeg-n", "ussagus", "utphys", "vak",
|
51
|
+
"vancouver", "worlddev", "xagsm", "xplain", "zharticle"].map{|style| style.to_sym})
|
52
|
+
|
53
|
+
@@citation_styles_csl = Set.new(CSLAdaptor.list)
|
54
|
+
|
55
|
+
class << self
|
56
|
+
attr_accessor :citation_styles_bst, :citation_styles_csl
|
57
|
+
|
58
|
+
def stylize_citations(article, bibliography, export_style, citation_style, options = {})
|
59
|
+
# nothing to do if no bibliography is given
|
60
|
+
return article if bibliography.to_s.empty?
|
61
|
+
|
62
|
+
# The citation style we'll use for this export run is either:
|
63
|
+
citation_style = citation_style.to_s.to_sym
|
64
|
+
if !(@@citation_styles_csl.member?(citation_style) || @@citation_styles_bst.member?(citation_style))
|
65
|
+
# The citation style isn't recognized, use a default for the style - the default for the article would've been passed in
|
66
|
+
citation_style = export_style.citation_style || # 1. Provided by the export style specification
|
67
|
+
:plain # 2. The plain citation style as an ultimate fallback
|
68
|
+
end
|
69
|
+
@bib_processor = if @@citation_styles_csl.member? citation_style
|
70
|
+
:citeproc
|
71
|
+
elsif @@citation_styles_bst.member? citation_style
|
72
|
+
:bibtex
|
73
|
+
else
|
74
|
+
# Fallback to using citeproc processing, as it is faster and simpler
|
75
|
+
:citeproc
|
76
|
+
end
|
77
|
+
|
78
|
+
# Bibtex requires some extra latex definitions:
|
79
|
+
case @bib_processor
|
80
|
+
when :bibtex
|
81
|
+
article << "\n\n"
|
82
|
+
article << "\\bibliographystyle{#{citation_style}}\n"
|
83
|
+
# TODO: The dfgproposal treatment is needed for any template using BibLaTeX
|
84
|
+
# for typesetting bibliographies; this is a first of potentially many
|
85
|
+
article << case export_style.symbol
|
86
|
+
when :dfgproposal
|
87
|
+
# The \\printbibliography needs to be in the Bibliography section, which is NOT
|
88
|
+
# at the end of the article. So we disable it entirely here.
|
89
|
+
# article << "\\printbibliography\n\n"
|
90
|
+
""
|
91
|
+
when :plos2015 # disable line numbers for PLOS bibliographies
|
92
|
+
"\\nolinenumbers\n\\bibliography{#{bibliography}}\n\n"
|
93
|
+
else
|
94
|
+
"\\bibliography{#{bibliography}}\n\n"
|
95
|
+
end
|
96
|
+
when :citeproc
|
97
|
+
bibtex = begin
|
98
|
+
BibTeX.open(bibliography)
|
99
|
+
rescue => e
|
100
|
+
# TODO: Return errors, without fully failing
|
101
|
+
puts "Failed to fill in citations due to errors in your Bibliography #{bibliography}: #{e}"
|
102
|
+
nil
|
103
|
+
end
|
104
|
+
# Pandoc can't handle \hyperref links, so don't decorate for the word export.
|
105
|
+
article = CSLAdaptor.replace_citations_with_csl(article, citation_style, bibtex, decorate: options["decorate"])
|
106
|
+
end
|
107
|
+
|
108
|
+
return article
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
end
|
@@ -0,0 +1,147 @@
|
|
1
|
+
# Motivation for this adaptor can be seen at: https://github.com/inukshuk/csl-ruby/issues/6
|
2
|
+
require 'citeproc/ruby'
|
3
|
+
require 'csl/styles'
|
4
|
+
require 'bibtex'
|
5
|
+
require 'texstylist/csl_constants'
|
6
|
+
require 'texstylist/latex_util'
|
7
|
+
|
8
|
+
class CSLAdaptor
|
9
|
+
|
10
|
+
# Borrowing from https://github.com/inukshuk/jekyll-scholar/blob/master/lib/jekyll/scholar/utilities.rb#L5
|
11
|
+
# until CSL features stabilize
|
12
|
+
#
|
13
|
+
# Load styles into static memory.
|
14
|
+
# They should be thread safe as long as they are
|
15
|
+
# treated as being read-only.
|
16
|
+
STYLES = Hash.new do |h, k|
|
17
|
+
style = CSL::Style.load k
|
18
|
+
style = style.independent_parent unless style.independent?
|
19
|
+
h[k.to_s] = style
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.list
|
23
|
+
Dir.glob(File.join(CSL::Style.root,'**','*.csl')).map{|p| File.basename(p,".*").to_sym}
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.safe_style(style)
|
27
|
+
if style.is_a? Symbol
|
28
|
+
style = style.to_s
|
29
|
+
end
|
30
|
+
style_path = File.basename(style,".*") + '.csl'
|
31
|
+
expected_path = File.join(CSL::Style.root,style_path)
|
32
|
+
dependent_path = File.join(CSL::Style.root,'dependent',style_path)
|
33
|
+
if File.exist?(expected_path)
|
34
|
+
style
|
35
|
+
elsif File.exist?(dependent_path)
|
36
|
+
# While waiting for the main CSL library to implement dependent support, we'll pass the parent here
|
37
|
+
begin
|
38
|
+
dom = Nokogiri::XML(File.open(dependent_path))
|
39
|
+
parent_link = dom.search('link[@rel="independent-parent"]').first.attr('href')
|
40
|
+
parent_style = parent_link.sub('http://www.zotero.org/styles/','')
|
41
|
+
rescue
|
42
|
+
:'chicago-author-date'
|
43
|
+
end
|
44
|
+
else
|
45
|
+
:'chicago-author-date'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def self.load(style)
|
50
|
+
style = safe_style(style)
|
51
|
+
begin
|
52
|
+
style.present? && CSL::Style.load(style)
|
53
|
+
rescue
|
54
|
+
nil
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.citation_style_names
|
59
|
+
CSLConstants.citation_style_names
|
60
|
+
end
|
61
|
+
def self.citation_style_symbols
|
62
|
+
HashWithIndifferentAccess.new(self.citation_style_names.invert)
|
63
|
+
end
|
64
|
+
|
65
|
+
def self.replace_citations_with_csl(text, citation_style, bibtex, options={})
|
66
|
+
options = {decorate: true}.merge(options)
|
67
|
+
citation_style = CSLAdaptor.safe_style(citation_style)
|
68
|
+
renderer = CiteProc::Ruby::Renderer.new(format: 'text', style: citation_style)
|
69
|
+
# Dependent styles still experience issues, use the default chicago processor as a fallback
|
70
|
+
default_renderer = CiteProc::Ruby::Renderer.new(format: 'text', style: :'chicago-author-date')
|
71
|
+
|
72
|
+
csl_unique_count = 0
|
73
|
+
csl_map = {}
|
74
|
+
latex_util = LatexUtil.new
|
75
|
+
references_section = "\\section*{References}\n"
|
76
|
+
text = latex_util.preprocess_verb(text)
|
77
|
+
text = text.gsub(LatexUtil.citation_regex) do |match|
|
78
|
+
cite_type = $~[:type]
|
79
|
+
star = $~[:star]
|
80
|
+
optional_arg1 = $~[:opt1]
|
81
|
+
optional_arg2 = $~[:opt2]
|
82
|
+
braces = $~[:braces]
|
83
|
+
|
84
|
+
citations = braces.split(',').flatten
|
85
|
+
citations = citations.map {|c| c.strip}
|
86
|
+
length = citations.length
|
87
|
+
csl_text = citations.map do |c|
|
88
|
+
new_unique = !csl_map[c]
|
89
|
+
if new_unique
|
90
|
+
csl_unique_count += 1
|
91
|
+
csl_map[c] = csl_unique_count
|
92
|
+
end
|
93
|
+
csl_index = csl_map[c]
|
94
|
+
|
95
|
+
bib_data = !c.empty? && bibtex && bibtex[c.to_sym]
|
96
|
+
if bib_data.nil? # fallback - no such bib entry
|
97
|
+
'(missing citation)'
|
98
|
+
else
|
99
|
+
item = CiteProc::CitationItem.new id: c do |ci|
|
100
|
+
ci.data = CiteProc::Item.new bib_data.to_citeproc
|
101
|
+
# numeric styles not yet implemented in citeproc-ruby, so we need to manually set the number, see:
|
102
|
+
# https://github.com/inukshuk/citeproc-ruby/issues/40
|
103
|
+
ci.data[:'citation-number'] = csl_index
|
104
|
+
end
|
105
|
+
# I. If just added citation, add it to final Bibliography
|
106
|
+
if new_unique
|
107
|
+
begin # sometimes the CSL style has no bibliography definition, and the references render raises exceptions
|
108
|
+
rendered_reference = renderer.render item, STYLES[citation_style].bibliography
|
109
|
+
if options[:decorate]
|
110
|
+
references_section << "\\phantomsection\n\\label{csl:#{csl_unique_count}}"
|
111
|
+
end
|
112
|
+
references_section << rendered_reference
|
113
|
+
references_section << "\n\n"
|
114
|
+
rescue => e
|
115
|
+
puts "CSL bibliography render failed with: ", e
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
# II. Always add the inline rendered citation
|
120
|
+
begin
|
121
|
+
inline_render = renderer.render [item], STYLES[citation_style].citation
|
122
|
+
if inline_render.blank?
|
123
|
+
inline_render = begin
|
124
|
+
default_renderer.render [item], STYLES[citation_style].citation
|
125
|
+
end
|
126
|
+
if inline_render.blank?
|
127
|
+
inline_render = '(missing citation)'
|
128
|
+
end
|
129
|
+
end
|
130
|
+
if options[:decorate]
|
131
|
+
"\\hyperref[csl:#{csl_index}]{#{inline_render}}"
|
132
|
+
else
|
133
|
+
inline_render
|
134
|
+
end
|
135
|
+
rescue => e
|
136
|
+
puts "CSL citation render failed with: ", e
|
137
|
+
""
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
csl_text.join(" ")
|
142
|
+
end
|
143
|
+
return latex_util.postprocess_verb(text) + "\n\n" + references_section
|
144
|
+
end
|
145
|
+
|
146
|
+
|
147
|
+
end
|