I have this rules in my GNU Makefile:

FITXER = fitxa.md

$(FITXER).html: $(FITXER)
    pandoc --from markdown --to html $(FITXER) -o $(FITXER).html

$(FITXER).jpeg: $(FITXER).html
    wkhtmltoimage $(FITXER).html $(FITXER).jpeg

Is there any way to apply this rules in a list of files: for example something like that (in pseudocode):

for FITXER in [fitxa.md, a.md, b.md, ...] do

   $(FITXER).html: $(FITXER)
    pandoc --from markdown --to html $(FITXER) -o $(FITXER).html

   $(FITXER).jpeg: $(FITXER).html
    wkhtmltoimage $(FITXER).html $(FITXER).jpeg

The way to go about this is to define general build rules:

%.html: %.md
    pandoc --from markdown --to html $< -o $@

%.jpg: %.html
    wkhtmltoimage $< $@

This tells make how to create HTML files from Markdown files, then how to create JPEGs from HTML files.

Once you've done that, all that's needed is to tell make all the output files you're after:

all: fitxa.jpg a.jpg b.jpg

.PHONY: all

make all will figure out what to do to obtain the requested JPEG files.

You can still define other rules to process specific Markdown files in a different way:

foo.jpg: foo.md
    # Process foo.md here to build foo.jpg

These specific rules take precedence over the generic rules.

  • Thanks but your example applies for all files with an extensions .html and .md but what about if you want to apply this rules just to some documents in our list? That is, perhaps a.md to a.html need a pandoc but foo.md need another tool. Don't I explain well? In my example I just want to apply to the files fitxa.md, a.md and b.md. – somenxavier Nov 18 '16 at 18:13
  • The example explains how to build an HTML file from a Markdown file, it doesn't apply to all files automatically. Make works backwards from the target, all in the example, so it only builds fitxa.jpg, a.jpg and b.jpg, regardless of what Markdown or HTML files are present. You can also specify other, non-generic rules for other files. – Stephen Kitt Nov 18 '16 at 19:03
  • How can I do that? How make that non-generic rules are applied to foo.md for example? – somenxavier Nov 19 '16 at 22:26
  • See my update. May I suggest you read the GNU Make manual, it will help you understand how to write rules. – Stephen Kitt Nov 20 '16 at 16:32
  • 1
    Yes, there are such control statements, but you don't need them for the problem as stated in your question. Make works better if you let it handle dependencies by using it declaratively instead of using it as a scripting language. – Stephen Kitt Nov 20 '16 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.