-
Notifications
You must be signed in to change notification settings - Fork 28
Js Port #83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Js Port #83
Changes from 25 commits
8b172ec
16e971c
0a34799
f304423
8a32a2e
50318b4
e07b20a
739381b
b792377
720063a
81e07c7
87e794e
6e99f2a
227a0fd
b0d0e7b
cba9e46
06128bc
1ac728f
9d23ffe
83f42bb
b201866
0f47302
b8ed5ea
b1ea41d
6f6422b
0564033
6b8c909
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,7 @@ | |
/samples/*/*.html | ||
/samples/*/*.pdf | ||
pkg | ||
.bundle | ||
node_modules | ||
dist | ||
sample | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[submodule "vendor/locales"] | ||
path = vendor/locales | ||
url = https://github.com/citation-style-language/locales.git | ||
branch = master | ||
Leon0402 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[submodule "vendor/styles"] | ||
path = vendor/styles | ||
url = https://github.com/citation-style-language/styles.git | ||
branch = master |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
module AsciidoctorBibtex | ||
`const {Cite, plugins} = require('@citation-js/core')` | ||
`require('@citation-js/plugin-bibtex')` | ||
require "native" | ||
|
||
module BibTeX | ||
def self.open(path, options = {}) | ||
file = File.read(path, encoding: 'utf-8') | ||
return Bibliography.new(`new Cite(#{file})`) | ||
end | ||
|
||
class Bibliography | ||
def initialize(js_bibliography) | ||
@js_bibliography = js_bibliography | ||
|
||
@entries = Hash.new(%x{#{@js_bibliography}.format('bibtex', { format: 'object'}).reduce((map, cite) => { | ||
map[cite.label] = cite; | ||
return map; | ||
}, {})}) | ||
@entries = @entries.transform_values! { |bibtex_entry| Entry.new(bibtex_entry) } | ||
end | ||
|
||
def to_citeproc(options = {}) | ||
return @js_bibliography | ||
Leon0402 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
def [](key) | ||
return @entries[key] | ||
end | ||
end | ||
|
||
class Entry | ||
attr_reader :author, :editor, :year | ||
|
||
def initialize(bibtex_entry) | ||
@author = bibtex_entry[:properties][:author] | ||
@editor = bibtex_entry[:properties][:editor] | ||
@year = bibtex_entry[:properties][:year] | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
module AsciidoctorBibtex | ||
`const {Cite, plugins} = require('@citation-js/core')` | ||
`require('@citation-js/plugin-csl')` | ||
`const { styles } = require('csl-js')` | ||
`const path = require('path')` | ||
|
||
module CiteProc | ||
class Processor | ||
attr_reader :style, :format, :locale | ||
|
||
def initialize(options) | ||
@style = options[:style] | ||
@format = options[:format] | ||
@locale = options[:locale] | ||
|
||
styleFilePath = "../vendor/styles/#{@style}.csl" | ||
styleFilePath = `path.resolve(__dirname, #{styleFilePath})` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Mogztter I needed to add this, so it works regardless of the structure. I'm sure this can be written in ruby, but I'm not quite sure how. I tried a few things like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it might be a bug in opal. I tried following code snippet:
With ruby Wit opal (our version & newest) you get the same behaviour for So it seems There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better to use File.absolute_path. File.expand_path has some magic behavior that isn't needed here. |
||
raise "bibtex-style '#{@style}' does not exist" unless File.file?(styleFilePath) | ||
|
||
localeFilePath = "../vendor/locales/locales-#{@locale}.xml" | ||
localeFilePath = `path.resolve(__dirname, #{localeFilePath})` | ||
raise "bibtex-locale '#{@locale}' does not exist" unless File.file?(localeFilePath) | ||
|
||
styleFile = File.read(styleFilePath, encoding: 'utf-8') | ||
localeFile = File.read(localeFilePath, encoding: 'utf-8') | ||
%x{ | ||
let csl_config = plugins.config.get('@csl') | ||
csl_config.templates.add(#{style}, #{styleFile}) | ||
csl_config.locales.add(#{locale}, #{localeFile}) | ||
|
||
// This is used for style_utils.rb as the lib itself doesn't expose infos about the csl styles | ||
styles.set(#{style}, #{styleFile}) | ||
} | ||
end | ||
|
||
def import(js_bibliography) | ||
@js_bibliography = js_bibliography | ||
end | ||
|
||
def render(mode, cite_data) | ||
case mode | ||
when :bibliography | ||
return %x{#{@js_bibliography}.format('bibliography', { | ||
entry: #{cite_data[:id]}, | ||
template: #{@style}, | ||
lang: #{@locale} | ||
})}, "" | ||
when :citation | ||
return %x{#{@js_bibliography}.format('citation', { | ||
entry: #{cite_data[:id]}, | ||
template: #{@style}, | ||
lang: #{@locale} | ||
})} | ||
else | ||
raise ArgumentError, "cannot render unknown mode: #{mode.inspect}" | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module AsciidoctorBibtex | ||
`const { styles } = require('csl-js')` | ||
module StyleUtils | ||
def self.is_numeric?(style) | ||
return `styles.get(#{style}).info.category['citation-format'] === 'numeric'` | ||
end | ||
end | ||
end |
Uh oh!
There was an error while loading. Please reload this page.