diff options
-rw-r--r-- | src/Site.hs | 18 |
1 files 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 | |||
5 | import Data.Monoid (Monoid(..), mconcat, (<>)) | 5 | import Data.Monoid (Monoid(..), mconcat, (<>)) |
6 | import Control.Monad (liftM, forM_, (<=<)) | 6 | import Control.Monad (liftM, forM_, (<=<)) |
7 | import Data.Char (toLower, isSpace, isAlphaNum) | 7 | import Data.Char (toLower, isSpace, isAlphaNum) |
8 | import Data.Maybe (mapMaybe, fromMaybe) | 8 | import Data.Maybe (mapMaybe, fromMaybe, listToMaybe) |
9 | import Data.Map (Map) | 9 | import Data.Map (Map) |
10 | import qualified Data.Map as Map | 10 | import qualified Data.Map as Map |
11 | import qualified Data.Set as Set | 11 | import qualified Data.Set as Set |
@@ -19,6 +19,7 @@ import Control.Applicative (Alternative(..), Applicative(..)) | |||
19 | import Text.Blaze.Html (toHtml, toValue, (!)) | 19 | import Text.Blaze.Html (toHtml, toValue, (!)) |
20 | import qualified Text.Blaze.Html5 as H | 20 | import qualified Text.Blaze.Html5 as H |
21 | import qualified Text.Blaze.Html5.Attributes as A | 21 | import qualified Text.Blaze.Html5.Attributes as A |
22 | import Text.Read (readMaybe) | ||
22 | 23 | ||
23 | import System.FilePath (takeBaseName, (</>), (<.>)) | 24 | import System.FilePath (takeBaseName, (</>), (<.>)) |
24 | 25 | ||
@@ -30,6 +31,8 @@ import Data.Char (toLower) | |||
30 | import Tex (compileTex) | 31 | import Tex (compileTex) |
31 | import Text.Printf (printf) | 32 | import Text.Printf (printf) |
32 | 33 | ||
34 | import Text.Regex.TDFA ((=~)) | ||
35 | |||
33 | main :: IO () | 36 | main :: IO () |
34 | main = hakyllWith config $ do | 37 | main = hakyllWith config $ do |
35 | match "templates/*" $ compile templateCompiler | 38 | match "templates/*" $ compile templateCompiler |
@@ -225,8 +228,17 @@ texTransform = walkM texTransformInline <=< walkM texTransformBlock | |||
225 | let | 228 | let |
226 | texId = texTranslation' $ texT tex | 229 | texId = texTranslation' $ texT tex |
227 | alignment <- loadSnapshotBody texId "alignment" | 230 | alignment <- loadSnapshotBody texId "alignment" |
228 | return $ printf "<object data=\"/%s\" type=\"image/svg+xml\" style=\"vertical-align:-%s\">%s</object>" | 231 | content <- loadBody texId |
229 | (toFilePath texId) (alignment :: String) tex | 232 | let |
233 | latexFontSize :: Double | ||
234 | latexFontSize = 12 / 1.25 | ||
235 | match = (=~) :: String -> String -> (String, String, String, [String]) | ||
236 | size = case match content "height='([-0-9\\.]+)pt' .* width='([-0-9\\.]+)pt'" of | ||
237 | (_, _, _, xs@[_, _]) -> (\[y, x] -> (x / latexFontSize, y / latexFontSize)) <$> mapM (readMaybe :: String -> Maybe Double) xs | ||
238 | _ -> Nothing | ||
239 | size' = maybe "" (\(a, b) -> printf " width:%.2fem; height:%.2fem;" a b) size | ||
240 | return $ printf "<object data=\"/%s\" type=\"image/svg+xml\" style=\"vertical-align:-%s;%s\">%s</object>" | ||
241 | (toFilePath texId) (alignment :: String) (size' :: String) tex | ||
230 | classOf DisplayMath = "display-math" | 242 | classOf DisplayMath = "display-math" |
231 | classOf InlineMath = "inline-math" | 243 | classOf InlineMath = "inline-math" |
232 | 244 | ||