-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GLI as CLI manager, add new convert command
- Loading branch information
1 parent
b928106
commit c4471e1
Showing
6 changed files
with
108 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,3 +33,5 @@ gem "pg", "~> 1.4" | |
gem "tqdm", "~> 0.4.1" | ||
|
||
gem "grim", "~> 1.3" | ||
|
||
gem "gli", "~> 2.21" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'gli' | ||
|
||
module AnyStyleWorkflowCLI | ||
extend GLI::App | ||
desc "Convert between different formats" | ||
command :convert do |c| | ||
c.flag [:x, 'from-xml'], type: String, desc: 'Path to anystyle parser xml file or directory containing such files' | ||
c.switch [:r, :recursive], desc: 'If input path is a directory, recurse into subfolders' | ||
c.flag [:c, 'to-csl'], type: String, desc: 'Path to output file in CSL-JSON format' | ||
c.switch ['add-file-id'], desc: 'Add the file name as id for the source of the citation' | ||
c.switch ['add-raw-citation'], desc: 'Add the file name as id for the source of the citation' | ||
|
||
c.action do |global_options, options, args| | ||
|
||
# input | ||
input_path = options['from-xml'] | ||
if File.file? input_path | ||
files = [input_path] | ||
elsif File.directory? input_path | ||
if options[:recursive] | ||
files = Dir.glob(File.join(input_path, '**', '*.xml')) | ||
else | ||
files = Dir.glob(File.join(input_path, '*.xml')) | ||
end | ||
else | ||
raise 'Invalid input path' | ||
end | ||
|
||
mapped_ds = {} | ||
as = Datamining::AnyStyle.new(use_default_models:true) | ||
files.each do |file_path| | ||
xml = File.read(file_path, encoding:'utf-8').gsub('', '') | ||
mapped_ds[file_path] = as.xml_to_wapiti(xml) | ||
end | ||
|
||
raise "No input given" if mapped_ds.empty? | ||
|
||
# output | ||
output_path = options['to-csl'] | ||
raise 'No output path given' if output_path.nil? | ||
json = [] | ||
mapped_ds.each do |file_path, ds| | ||
raw_citations = ds.to_txt(separator: "\n\n").split("\n\n") | ||
as.wapiti_to_csl(ds).each_with_index do |item, index| | ||
item['x-citation-source-id'] = File.basename(file_path, File.extname(file_path)) if options['add-file-id'] | ||
item['x-raw-citation'] = raw_citations[index] if options['add-raw-citation'] | ||
json.append item | ||
end | ||
end | ||
csl_json = JSON.pretty_generate(json) | ||
File.write(output_path, csl_json, encoding:'utf-8') | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env ruby | ||
|
||
require 'gli' | ||
require_relative 'lib/bootstrap' | ||
|
||
module AnyStyleWorkflowCLI | ||
extend GLI::App | ||
program_desc 'AnyStyle reference extraction workflow' | ||
|
||
|
||
# auto-register commands | ||
Dir[File.join(__dir__, 'commands', '*.rb')].each { |file| require file } | ||
end | ||
|
||
AnyStyleWorkflowCLI.run(ARGV) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters