inukshuk / citeproc-ruby

A Citation Style Language (CSL) Cite Processor
101 stars 22 forks source link

Append / prepend string after render? #51

Closed sgroth closed 6 years ago

sgroth commented 6 years ago

I am using citeproc-ruby to create a markdown-version of my list of publications. I know that the csl-file is supposed to control the bibliography layout, yet I am running into the problem that the line breaks of the output are not recognized by markdown (one space instead of two).

Is there a way to append a space / line break to each of the entries after render?

# Open bibliography
bib = BibTeX.open('bibliography.bib').convert(:latex)

# Create a new processor with the desired style, # format, and locale.
cp = CiteProc::Processor.new style: 'chicago-author-date', format: 'text', locale: 'de'

cp.import bib.to_citeproc

# Process entries with keyword "submitted"
submitted = bib['@*[keywords=submitted]'].map do |e|
  cp.render :bibliography, :id => e.key
end

# Write to output file
File.open( 'publications.de.markdown', "w" ) do |file|
  file.puts submitted
end
inukshuk commented 6 years ago

Sure. cp.render returns a string so you can append or prepend anything you want. There are many ways to do this, but for example:

"prefix#{cp.render :bibliography, id: e.key}suffix"

Alternatively, you could add the extra space (or linebreak?) when you write out the submitted array. For instance:

file.puts submitted.join("\n\n")
sgroth commented 6 years ago

Many thanks! I quick (and likely stupid) follow-up: if I do this

submitted = bib['@*[keywords=submitted]'].map do |e|
  "* #{cp.render :bibliography, id: e.key}\n"
end

File.open( 'publications.de.markdown', "w" ) do |file|
  file.puts submitted
end

the resulting markdown looks like this:

* ["correctly formatted entry"]

Is there a way to output the string without the enclosing [" "] brackets and quotes?

inukshuk commented 6 years ago

You're currently sending an array of strings to puts -- instead, remove the extra newline from your strings and print them using puts submitted.join("\n").

sgroth commented 6 years ago

But how is it possible to prepend something to each string? "* #{cp.render :bibliography, id: e.key}" combined with puts submitted.join("\n") results in * ["correctly formatted entry"] with brackets and quotes.

(Please feel free to ignore my question, I know it is not related to citeproc-ruby and caused by my limited Ruby knowledge.)

inukshuk commented 6 years ago

Oh wait, actually I did not notice you were using the engine's render method which returns an array (because you could render more than one reference) in this case. So you'll want to do "prefix#{cp.render(:bibliography, id: e.key)[0]}" -- sorry about misleading you!

sgroth commented 6 years ago

Perfect, many thanks!