From 36244ed4d5d16aecf7c10f01c4fb1aa0323f909d Mon Sep 17 00:00:00 2001 From: Gregor Kleen Date: Thu, 11 Feb 2016 15:31:15 +0100 Subject: Size hints in embedded svgs --- src/Site.hs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/Site.hs b/src/Site.hs index bf501d8..9b08316 100644 --- a/src/Site.hs +++ b/src/Site.hs @@ -5,7 +5,7 @@ import Hakyll import Data.Monoid (Monoid(..), mconcat, (<>)) import Control.Monad (liftM, forM_, (<=<)) import Data.Char (toLower, isSpace, isAlphaNum) -import Data.Maybe (mapMaybe, fromMaybe) +import Data.Maybe (mapMaybe, fromMaybe, listToMaybe) import Data.Map (Map) import qualified Data.Map as Map import qualified Data.Set as Set @@ -19,6 +19,7 @@ import Control.Applicative (Alternative(..), Applicative(..)) import Text.Blaze.Html (toHtml, toValue, (!)) import qualified Text.Blaze.Html5 as H import qualified Text.Blaze.Html5.Attributes as A +import Text.Read (readMaybe) import System.FilePath (takeBaseName, (), (<.>)) @@ -30,6 +31,8 @@ import Data.Char (toLower) import Tex (compileTex) import Text.Printf (printf) +import Text.Regex.TDFA ((=~)) + main :: IO () main = hakyllWith config $ do match "templates/*" $ compile templateCompiler @@ -225,8 +228,17 @@ texTransform = walkM texTransformInline <=< walkM texTransformBlock let texId = texTranslation' $ texT tex alignment <- loadSnapshotBody texId "alignment" - return $ printf "%s" - (toFilePath texId) (alignment :: String) tex + content <- loadBody texId + let + latexFontSize :: Double + latexFontSize = 12 / 1.25 + match = (=~) :: String -> String -> (String, String, String, [String]) + size = case match content "height='([-0-9\\.]+)pt' .* width='([-0-9\\.]+)pt'" of + (_, _, _, xs@[_, _]) -> (\[y, x] -> (x / latexFontSize, y / latexFontSize)) <$> mapM (readMaybe :: String -> Maybe Double) xs + _ -> Nothing + size' = maybe "" (\(a, b) -> printf " width:%.2fem; height:%.2fem;" a b) size + return $ printf "%s" + (toFilePath texId) (alignment :: String) (size' :: String) tex classOf DisplayMath = "display-math" classOf InlineMath = "inline-math" -- cgit v1.2.3