<commit_before>puts "Initializing..."

require 'citeproc'
require 'bibtex'
require 'ruby-progressbar'

BIBTEX_PATH = 'report.bib'
OUTPUT_PATH = 'output/report.latex'

report = File.open(OUTPUT_PATH, "w")
HEADER = [
	"\\tableofcontents"
]
HEADER.each { |e| report.puts e }

puts "Loading bibliography..."
bibliography = BibTeX.open(BIBTEX_PATH, :strip => false)
storage = []
key_list = []

puts "Generating citations..."
prog_bar = ProgressBar.create(:title => "Entries written", :starting_at => 0, :total => bibliography.count, :format => '%c |%b>>%i| %p%% %t')	# => Create a progress bar

bibliography.each do |entry|
	citation = CiteProc.process(entry.to_citeproc, :style => :apa)
	notes = entry[:annote]
	notes = notes.gsub(/$/,"\n\n") unless notes.nil?
	key = entry[:keywords].to_s
	key_list << key

	storage << {
		:key => key,
		:citation => citation,
		:notes => notes
	}
	prog_bar.increment
end

puts "#{key_list.count} total keys"
key_list.uniq!
puts "#{key_list.count} different keys."

puts "Writing out to latex..."
for key in key_list do
	report.puts "\\section{#{key}}"
	storage.select { |value| value[:key] == key }.each do |entry|
		report.puts "\\subsection{#{entry[:citation]}}"
		report.puts entry[:notes]
	end
end
	

puts "Generating pdf..."
`pandoc output/report.latex -o output/report.pdf --latex-engine=xelatex`
`open output/report.pdf`<commit_msg>Add comments<commit_after>###########
#
# This script takes a BibLaTeX file exported from Zotero and generates
# a PDF report of citations and their notes, sorted by tag.
#
#
###########

require 'citeproc'
require 'bibtex'
require 'ruby-progressbar'

BIBTEX_PATH = 'report.bib'
OUTPUT_PATH = 'output/report.latex'

# Add any desired frontmatter to the report file
report = File.open(OUTPUT_PATH, "w")
HEADER = [
]
HEADER.each { |e| report.puts e }

puts "Loading bibliography..."
bibliography = BibTeX.open(BIBTEX_PATH, :strip => false)
storage = []
key_list = []

puts "Generating citations..."
prog_bar = ProgressBar.create(:title => "Entries written", :starting_at => 0, :total => bibliography.count, :format => '%c |%b>>%i| %p%% %t')	# => Create a progress bar

# Generate a citation via CiteProc for each bibliographic entry, and
# retrieve&format its annotations. These are put into a temporary array until
# the list of unique tags is determined

bibliography.each do |entry|
	citation = CiteProc.process(entry.to_citeproc, :style => :apa)
	notes = entry[:annote]
	# Exported notes only have single line breaks between paragraphs. LaTeX
	# requires double line breaks
	notes = notes.gsub(/$/,"\n\n") unless notes.nil?
	# Retrieve the entry keyword and store it in a temporary array
	key = entry[:keywords].to_s
	key_list << key

	storage << {
		:key => key,
		:citation => citation,
		:notes => notes
	}
	prog_bar.increment
end

key_list.uniq!
puts "#{key_list.count} unique sections."

# Write a section for each key, and put individual citations with that key
# into each subsection
puts "Writing out to latex..."
for key in key_list do
	report.puts "\\section{#{key}}"
	storage.select { |value| value[:key] == key }.each do |entry|
		report.puts "\\subsection{#{entry[:citation]}}"
		report.puts entry[:notes]
	end
end

puts "Generating pdf..."
`pandoc output/report.latex -o output/report.pdf --latex-engine=xelatex`
`open output/report.pdf`
{'fraction_non_alphanumeric': 0.15282791817087846, 'fraction_numerical': 0.0006016847172081829, 'mean_word_length': 4.486798679867987, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '203872', 'n_tokens_mistral': 1193, 'n_tokens_neox': 1130, 'n_words': 333}