Skip to content

Commit

Permalink
Support underscore in the exporters on titles
Browse files Browse the repository at this point in the history
Underscores on titles are meant to be replaced by something else on each
exporter (e.g. removed on CSV, or surrounded by \emph on UOC).

Signed-off-by: Miquel Sabaté Solà <[email protected]>
  • Loading branch information
mssola committed Mar 20, 2024
1 parent 120658d commit 2cf0ffb
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
14 changes: 11 additions & 3 deletions lib/csv_exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,18 @@ def export
str
end

protected

# Generate a new CSV line from the given `thing`.
def thing_to_csv(thing:)
CSV.generate_line([thing.target, thing.authors, thing.editors, thing.year, thing.title,
thing.note, thing.insideof, thing.url, thing.publisher, thing.kind,
thing.access, thing.bought_at],
CSV.generate_line([thing.target, thing.authors, thing.editors, thing.year,
clean_title(title: thing.title), thing.note, thing.insideof,
thing.url, thing.publisher, thing.kind, thing.access, thing.bought_at],
quote_empty: false)
end

# Clean the given title from underscores and other such characters.
def clean_title(title:)
title.delete('_')
end
end
21 changes: 20 additions & 1 deletion lib/uoc_exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def export
# Returns the head of a line, containing authors, year, title and, if
# available, the "inside of" information.
def head(thing:)
str = "#{parse_authors(thing:)} (#{thing.year}). \\emph{#{thing.title}}."
str = "#{parse_authors(thing:)} (#{thing.year}). #{parse_title(title: thing.title)}."

if thing.insideof.present?
str += " #{inside_of(thing.insideof)}"
Expand All @@ -26,6 +26,25 @@ def head(thing:)
str
end

# Returns the title without underscores and with the expected formatting.
def parse_title(title:)
str = ''
seen = false

# I'm sure there is a clever (and comboluted) regexp that can also achieve
# this, but after some tries I decided to cut the crap.
title.each_char do |c|
if c == '_'
str += seen ? '\emph{' : '}'
seen = !seen
else
str += c
end
end

"\\emph{#{str}}"
end

# Parse the authors as delivered by `thing.authors` and render them in the
# proper format.
def parse_authors(thing:)
Expand Down
18 changes: 18 additions & 0 deletions test/controllers/exports_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,28 @@ class ExportsControllerTest < ActionDispatch::IntegrationTest
assert_equal 'text/csv', @response.media_type
end

test 'csv: underscore is removed for the title' do
things(:thing1).update!(title: 'This is a _title_ with _many_ underscores')

get search_exports_url(0, format: 'csv')

body = @response.body.split("\n")

assert_equal 'This is a title with many underscores', body.last.split(',')[7]
end

test 'uoc: works' do
get search_exports_url(0, format: 'uoc')

assert_equal @response.body, file_fixture('all.uoc.tex').read
assert_equal 'application/x-tex', @response.media_type
end

test 'uoc: underscore is supported for the title' do
things(:thing1).update!(title: 'This is a _title_ with _many_ underscores')

get search_exports_url(0, format: 'uoc')

assert_equal @response.body, file_fixture('underscores.uoc.tex').read
end
end
9 changes: 9 additions & 0 deletions test/fixtures/files/underscores.uoc.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
\chapter{Bibliografia}

{\setlength{\parskip}{-0.3cm}

\hypertarget{target2}{\textsc{Sabaté}, Miquel; \textsc{Nom} (eds.) (2024). \emph{This is a 2}. Dins de \emph{Some other thing}, 22-24. This is a note 2. Publisher 2, Address 2: 2024.}

\\~\\\hypertarget{target1}{\textsc{Sabaté}, Miquel; \textsc{Smith}, John; \textsc{cognom2}, Nom cognom1; \textsc{cognom3 cognom4}, Nom (2024). \emph{This is a }title\emph{ with }many\emph{ underscores}. Dins de \emph{Some other thing}, 22-24. This is a note 1. Publisher 1. [\url{http://example.com}]. [Última data de consulta: 24 de febrer de 2024].}

}

0 comments on commit 2cf0ffb

Please sign in to comment.