From e32227f74482c8a7907c3557fcb42e7f35f0c610 Mon Sep 17 00:00:00 2001
From: John MacFarlane <jgm@berkeley.edu>
Date: Fri, 13 Feb 2015 23:29:12 -0800
Subject: [PATCH] Allow wildcards in `--epub-embed-font` arguments.

Closes #1939.
---
 README                          |  9 ++++++---
 pandoc.cabal                    |  3 ++-
 src/Text/Pandoc/Writers/EPUB.hs | 11 +++++++++--
 3 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/README b/README
index b54107f32..160fc539e 100644
--- a/README
+++ b/README
@@ -608,9 +608,12 @@ Options affecting specific writers
 
 `--epub-embed-font=`*FILE*
 :   Embed the specified font in the EPUB. This option can be repeated
-    to embed multiple fonts.  To use embedded fonts, you
-    will need to add declarations like the following to your CSS (see
-    `--epub-stylesheet`):
+    to embed multiple fonts.  Wildcards can also be used: for example,
+    `DejaVuSans-*.ttf`.  However, if you use wildcards on the command
+    line, be sure to escape them or put the whole filename in single quotes,
+    to prevent them from being interpreted by the shell. To use the
+    embedded fonts, you will need to add declarations like the following
+    to your CSS (see `--epub-stylesheet`):
 
         @font-face {
         font-family: DejaVuSans;
diff --git a/pandoc.cabal b/pandoc.cabal
index a788a1c26..16106f896 100644
--- a/pandoc.cabal
+++ b/pandoc.cabal
@@ -253,7 +253,8 @@ Library
                  haddock-library >= 1.1 && < 1.3,
                  old-time,
                  deepseq-generics >= 0.1 && < 0.2,
-                 JuicyPixels >= 3.1.6.1 && < 3.3
+                 JuicyPixels >= 3.1.6.1 && < 3.3,
+                 filemanip >= 0.3 && < 0.4
   if flag(old-locale)
      Build-Depends: old-locale >= 1 && < 1.1,
                     time >= 1.2 && < 1.5
diff --git a/src/Text/Pandoc/Writers/EPUB.hs b/src/Text/Pandoc/Writers/EPUB.hs
index cebbaa835..63c3b5501 100644
--- a/src/Text/Pandoc/Writers/EPUB.hs
+++ b/src/Text/Pandoc/Writers/EPUB.hs
@@ -36,6 +36,7 @@ import Data.List ( isPrefixOf, isInfixOf, intercalate )
 import System.Environment ( getEnv )
 import Text.Printf (printf)
 import System.FilePath ( takeExtension, takeFileName )
+import System.FilePath.Glob ( namesMatching )
 import qualified Data.ByteString.Lazy as B
 import qualified Data.ByteString.Lazy.Char8 as B8
 import qualified Text.Pandoc.UTF8 as UTF8
@@ -57,7 +58,7 @@ import Text.Pandoc.Options ( WriterOptions(..)
 import Text.Pandoc.Definition
 import Text.Pandoc.Walk (walk, walkM)
 import Control.Monad.State (modify, get, execState, State, put, evalState)
-import Control.Monad (foldM, mplus, liftM)
+import Control.Monad (foldM, mplus, liftM, when)
 import Text.XML.Light ( unode, Element(..), unqual, Attr(..), add_attrs
                       , strContent, lookupAttr, Node(..), QName(..), parseXML
                       , onlyElems, node, ppElement)
@@ -387,8 +388,14 @@ writeEPUB opts doc@(Pandoc meta _) = do
   picEntries <- foldM readPicEntry [] pics
 
   -- handle fonts
+  let matchingGlob f = do
+        xs <- namesMatching f
+        when (null xs) $
+          warn $ f ++ " did not match any font files."
+        return xs
   let mkFontEntry f = mkEntry (takeFileName f) `fmap` B.readFile f
-  fontEntries <- mapM mkFontEntry $ writerEpubFonts opts'
+  fontFiles <- concat <$> mapM matchingGlob (writerEpubFonts opts')
+  fontEntries <- mapM mkFontEntry fontFiles
 
   -- set page progression direction attribution
   let progressionDirection = case epubPageDirection metadata of