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 all 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
/samples/*/*.html
/samples/*/*.pdf
pkg
node_modules/
dist/
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 => 'd136ea8'
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
10 changes: 6 additions & 4 deletions lib/asciidoctor-bibtex/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
# Treeprocessor extension for asciidoctor
#

require 'asciidoctor'
require 'asciidoctor/extensions'
require 'asciidoctor/reader'
require 'asciidoctor/parser'
unless RUBY_PLATFORM == 'opal'
require 'asciidoctor'
require 'asciidoctor/extensions'
require 'asciidoctor/reader'
require 'asciidoctor/parser'
end

require_relative 'path_utils'
require_relative 'processor'
Expand Down
48 changes: 48 additions & 0 deletions lib/asciidoctor-bibtex/js/bibtex.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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

%x{
// TODO: Doesn't seem to work if the name contains a ":"
const config = plugins.config.get('@bibtex')
config.format.useIdAsLabel = true;
}

@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
60 changes: 60 additions & 0 deletions lib/asciidoctor-bibtex/js/citeproc.rb
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})`
Copy link
Author

Choose a reason for hiding this comment

The 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 styleFilePath == File.expand_path(styleFilePath, __FILE__).

Copy link
Member

@ggrossetie ggrossetie May 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What File.expand_path(styleFilePath, __FILE__) gives you? Maybe __FILE__ is not correctly implemented in Opal... 🤔

Copy link
Author

Choose a reason for hiding this comment

The 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:

puts __FILE__
puts File.expand_path('../vendor/styles',  __FILE__)

With ruby __FILE__ just gives the file name and expand_path the full absolute path.

Wit opal (our version & newest) you get the same behaviour for __FILE__, but expand_path just gives ./vendor/styles.

So it seems expand_path or __FILE__ is not correctly implemented.

Copy link
Member

Choose a reason for hiding this comment

The 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
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