Skip to content
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

Js Port #83

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8b172ec
Basic js setup
Leon0402 Apr 23, 2022
16e971c
Import asciidoctor only for ruby
Leon0402 Apr 23, 2022
0a34799
Add mocks for missing methods
Leon0402 Apr 23, 2022
f304423
WIP
Leon0402 Apr 23, 2022
8a32a2e
First working version
Leon0402 Apr 23, 2022
50318b4
Use entry output option
Leon0402 Apr 23, 2022
e07b20a
Remove unneeded files
Leon0402 Apr 24, 2022
739381b
Extract information from bitex format instead
Leon0402 Apr 24, 2022
b792377
Add style and locale option
Leon0402 Apr 24, 2022
720063a
Replace another mutable string
Leon0402 Apr 24, 2022
81e07c7
Support csl styles
Leon0402 Apr 24, 2022
87e794e
Add language support
Leon0402 Apr 24, 2022
6e99f2a
Fix numbering for numerical entries
Leon0402 Apr 24, 2022
227a0fd
Remove double import
Leon0402 Apr 24, 2022
b0d0e7b
Add error handling for unknown references
Leon0402 Apr 24, 2022
cba9e46
Fix typo in error message
Leon0402 Apr 24, 2022
06128bc
Use opal ref d136ea8
Leon0402 Apr 24, 2022
1ac728f
Merge branch 'feature/jsPort' of github.com:Leon0402/asciidoctor-bibt…
Leon0402 Apr 24, 2022
9d23ffe
Use one block for opal platform
Leon0402 Apr 24, 2022
83f42bb
Update package.json description
Leon0402 Apr 24, 2022
b201866
Update dev dependency in package.json
Leon0402 Apr 24, 2022
0f47302
Merge branch 'feature/jsPort' of github.com:Leon0402/asciidoctor-bibt…
Leon0402 Apr 24, 2022
b8ed5ea
Use File.file instead of native js code
Leon0402 Apr 24, 2022
b1ea41d
Build hash once
Leon0402 Apr 24, 2022
6f6422b
Resolve path to absolute
Leon0402 May 1, 2022
0564033
Resolve issue regarding gitignore
Leon0402 May 1, 2022
6b8c909
Use id as label
Leon0402 May 1, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@
/samples/*/*.html
/samples/*/*.pdf
pkg
.bundle
node_modules
dist
sample
Leon0402 marked this conversation as resolved.
Show resolved Hide resolved
8 changes: 8 additions & 0 deletions .gitmodules
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
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ source 'https://rubygems.org'
gemspec

gem 'unicode_utils' if (Gem::Version.new RUBY_VERSION) < (Gem::Version.new '2.4.0')

gem 'opal', :git => 'https://github.com/opal/opal.git', :ref => 'tags/v1.0.0'
Leon0402 marked this conversation as resolved.
Show resolved Hide resolved
22 changes: 22 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,25 @@ rescue LoadError
end

task :default => default_tasks unless default_tasks.empty?


JS_FILE = 'build/asciidoctor-bibtex.js'
DIST_FILE = 'dist/main.js'

task :js do
require 'opal'

builder = Opal::Builder.new(compiler_options: {
dynamic_require_severity: :error,
})
builder.append_paths 'lib'
builder.build 'asciidoctor-bibtex'

FileUtils.mkdir_p([File.dirname(JS_FILE), File.dirname(DIST_FILE)])
File.open(JS_FILE, 'w') do |file|
file << builder.to_s
end
File.binwrite "#{JS_FILE}.map", builder.source_map

FileUtils.cp JS_FILE, DIST_FILE, :verbose => true
end
6 changes: 3 additions & 3 deletions lib/asciidoctor-bibtex/citation_macro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ class CitationMacro
# matches a citation item (key + locator), such as 'Dan2012(99-100)'
CITATION_ITEM = /([^\s,()\[\]]+)(\([^)]*\))?/.freeze
# matches a citation list
CITATION_LIST_TAIL = /(\s*,\s*#{CITATION_ITEM})*/.freeze
CITATION_LIST = /(?:#{CITATION_ITEM}#{CITATION_LIST_TAIL})/.freeze
CITATION_LIST_TAIL = /(\s*,\s*#{CITATION_ITEM.source})*/.freeze
CITATION_LIST = /(?:#{CITATION_ITEM.source}#{CITATION_LIST_TAIL.source})/.freeze
CITATION_PRETEXT = /[^\[]*/.freeze
# matches the full citation macro
CITATION_MACRO = /(#{CITATION_TYPE}):(#{CITATION_PRETEXT})\[(#{CITATION_LIST})\]/.freeze
CITATION_MACRO = /(#{CITATION_TYPE.source}):(#{CITATION_PRETEXT.source})\[(#{CITATION_LIST.source})\]/.freeze

# Given a line, return a list of CitationData instances
# containing information on each set of citation information
Expand Down
8 changes: 4 additions & 4 deletions lib/asciidoctor-bibtex/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# Treeprocessor extension for asciidoctor
#

require 'asciidoctor'
require 'asciidoctor/extensions'
require 'asciidoctor/reader'
require 'asciidoctor/parser'
require 'asciidoctor' unless RUBY_PLATFORM == 'opal'
require 'asciidoctor/extensions' unless RUBY_PLATFORM == 'opal'
require 'asciidoctor/reader' unless RUBY_PLATFORM == 'opal'
require 'asciidoctor/parser' unless RUBY_PLATFORM == 'opal'
Leon0402 marked this conversation as resolved.
Show resolved Hide resolved

require_relative 'path_utils'
require_relative 'processor'
Expand Down
35 changes: 35 additions & 0 deletions lib/asciidoctor-bibtex/js/bibtex.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module AsciidoctorBibtex
`const {Cite, plugins} = require('@citation-js/core')`
`require('@citation-js/plugin-bibtex')`

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
end

def to_citeproc(options = {})
return @js_bibliography
Leon0402 marked this conversation as resolved.
Show resolved Hide resolved
end

def [](key)
return Entry.new(%x{#{@js_bibliography}.format('bibtex', { format: 'object'}).find(cite => cite.label === #{key})})
Leon0402 marked this conversation as resolved.
Show resolved Hide resolved
end
end

class Entry
attr_reader :author, :editor, :year

def initialize(js_entry)
@author = `js_entry.properties.author`
@editor = `js_entry.properties.editor`
@year = `js_entry.properties.year`
end
end
end
end
58 changes: 58 additions & 0 deletions lib/asciidoctor-bibtex/js/citeproc.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
module AsciidoctorBibtex
`const {Cite, plugins} = require('@citation-js/core')`
`require('@citation-js/plugin-csl')`
`const Fs = require('fs')`
Leon0402 marked this conversation as resolved.
Show resolved Hide resolved
`const { styles } = require('csl-js')`

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"
raise "nibtex-style '#{@style}' does not exist" unless `Fs.existsSync(#{styleFilePath})`
Leon0402 marked this conversation as resolved.
Show resolved Hide resolved

localeFilePath = "../vendor/locales/locales-#{@locale}.xml"
raise "bibtex-locale '#{@locale}' does not exist" unless `Fs.existsSync(#{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
8 changes: 8 additions & 0 deletions lib/asciidoctor-bibtex/js/style_utils.rb
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
Loading