2

I generate PDFs from Markdown files using Rake. If a Markdown file is filename.md, I like the PDF to be filename.md.pdf not filename.pdf, so that autocompletion works the way I like and so that it's clear what the source of the PDF file is.

I have this Rake file, which works fine.

MDFILES = FileList["*.md"]
PDFS = MDFILES.ext("pdf")

desc "Build PDFs of all chapters"
task :pdfs => PDFS

# Build PDFs from Markdown source
rule ".pdf" => ".md" do |t|
  sh "pandoc #{t.source} -o #{t.name}"
end

If I run rake pdfs or rake filename.pdf the PDFs are generated as expected, but the PDFs are named filename.pdf.

But I want the Rakefile to be this instead:

MDFILES = FileList["*.md"]
PDFS = MDFILES.ext("md.pdf")

desc "Build PDFs of all chapters"
task :pdfs => PDFS

# Build PDFs from Markdown source
rule "md.pdf" => ".md" do |t|
  sh "pandoc #{t.source} -o #{t.name}"
end

Running rake pdfs or rake filename.md.pdf returns the error Don't know how to build task 'filename.md.pdf'.

How can I produce filenames the way I want?

By the way, this type of rule works fine with Make, to wit:

%.md.pdf : %.md
        pandoc $< -o $@
Lincoln Mullen
  • 6,257
  • 4
  • 27
  • 30

1 Answers1

3

I've had a similar problem myself recently when I attempted to specify an extension with multiple dots in a rule. I solved it by using a different rule syntax as described here.

Try something like this for your rule:

rule( /\.md\.pdf$/ => [
    proc {|task_name| task_name.sub(/\.md\.pdf$/, '.md') }
]) do |t|
    sh "pandoc #{t.source} -o #{t.name}"
end
  • That worked great. Thanks for pointing out that alternative rule syntax. It's certainly more powerful, but a lot uglier than Make's. – Lincoln Mullen May 11 '13 at 17:05