diff --git a/CHANGELOG.md b/CHANGELOG.md
new file mode 100644
--- /dev/null
+++ b/CHANGELOG.md
@@ -0,0 +1,4 @@
+1.1.0.0
+=======
+
+* Initial extraction from `pdf-slave` package.
diff --git a/LICENSE b/LICENSE
new file mode 100644
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,30 @@
+Copyright Anton Gushcha (c) 2016
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials provided
+      with the distribution.
+
+    * Neither the name of Anton Gushcha nor the names of other
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/README.md b/README.md
new file mode 100644
--- /dev/null
+++ b/README.md
@@ -0,0 +1,6 @@
+pdf-slave-template
+==================
+
+Template format definition for [pdf-slave](http://hackage.haskell.org/package/pdf-slave) package. See it for full info.
+
+The package is extracte to allow building the template definition with GHCJS.
diff --git a/Setup.hs b/Setup.hs
new file mode 100644
--- /dev/null
+++ b/Setup.hs
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
diff --git a/pdf-slave-template.cabal b/pdf-slave-template.cabal
new file mode 100644
--- /dev/null
+++ b/pdf-slave-template.cabal
@@ -0,0 +1,39 @@
+name:                pdf-slave-template
+version:             1.1.0.0
+synopsis:            Template format definition for pdf-slave tool
+description:         Please see README.md
+homepage:            https://github.com/ncrashed/pdf-slave#readme
+license:             BSD3
+license-file:        LICENSE
+author:              Anton Gushcha
+maintainer:          ncrashed@gmail.com
+copyright:           Anton Gushcha 2016
+category:            Web
+build-type:          Simple
+extra-source-files:
+  CHANGELOG.md
+  README.md
+  stack.yaml
+cabal-version:       >=1.10
+
+library
+  hs-source-dirs:      src
+  exposed-modules:     Text.PDF.Slave.Template
+  build-depends:
+      base              >= 4.7    && < 5
+    , aeson             >= 0.11   && < 0.12
+    , base64-bytestring >= 1.0    && < 1.1
+    , bytestring        >= 0.10   && < 0.11
+    , containers        >= 0.5    && < 0.6
+    , text              >= 1.2    && < 1.3
+
+  default-language:    Haskell2010
+  default-extensions:
+    DeriveGeneric
+    OverloadedStrings
+    RecordWildCards
+    ScopedTypeVariables
+
+source-repository head
+  type:     git
+  location: https://github.com/ncrashed/pdf-slave
diff --git a/src/Text/PDF/Slave/Template.hs b/src/Text/PDF/Slave/Template.hs
new file mode 100644
--- /dev/null
+++ b/src/Text/PDF/Slave/Template.hs
@@ -0,0 +1,197 @@
+-- | Defines document template
+module Text.PDF.Slave.Template(
+    TemplateName
+  , TemplateInput
+  , TemplateBody
+  , TemplateBibtex
+  , DependencyBody
+  , BibTexBody
+  , TemplateDependency(..)
+  , Template(..)
+  , TemplateDependencyFile(..)
+  , TemplateFile(..)
+  ) where
+
+import Control.Monad (mzero)
+import Data.ByteString (ByteString)
+import Data.Monoid
+import Data.Text as T
+import Data.Aeson
+import GHC.Generics
+
+import qualified Data.ByteString.Base64 as B64
+import qualified Data.Map.Strict        as M
+import qualified Data.Text.Encoding     as T
+
+-- | Template unique name
+type TemplateName = Text
+
+-- | A template takes simple YAML document as input
+type TemplateInput = Value
+
+-- | Template body is text with .htex content
+type TemplateBody = Text
+
+-- | Template can define additional bibtex database
+type TemplateBibtex = Text
+
+-- | Dependency can be a binary file
+type DependencyBody = ByteString
+
+-- | Content of bibtex file
+type BibTexBody = Text
+
+-- | Template has different types of dependencies, each type of the dependecy
+-- has own affect on rendering pipe.
+data TemplateDependency =
+  -- | Bibtex file for references to other documents. Need call to bibtex
+    BibtexDep BibTexBody
+  -- | HTex file that need to be compiled to .tex file
+  | TemplateDep Template
+  -- | HTex file that need to be compiled to .pdf file
+  | TemplatePdfDep Template
+  -- | Any other file that doesn't need a compilation (listings, images, etc)
+  | OtherDep DependencyBody
+  deriving (Generic, Show)
+
+instance FromJSON TemplateDependency where
+  parseJSON val@(Object o) = do
+    depType <- o .: "type"
+    case T.toLower . T.strip $ depType of
+      "bibtex"       -> BibtexDep       <$> o .: "body"
+      "template"     -> TemplateDep     <$> parseJSON val
+      "template_pdf" -> TemplatePdfDep  <$> parseJSON val
+      "other"        -> do
+        (t :: Text) <- o .: "body"
+        either (\e -> fail $ "Cannot decode dependency body (base64): " <> e) (return . OtherDep) $
+          B64.decode . T.encodeUtf8 $ t
+      _ -> fail $ "Unknown template type " <> unpack depType
+  parseJSON _ = mzero
+
+instance ToJSON TemplateDependency where
+  toJSON d = case d of
+    BibtexDep body -> object [
+        "type" .= ("bibtex" :: Text)
+      , "body" .= body
+      ]
+    TemplateDep body -> let
+      Object o1 = object [ "type" .= ("template" :: Text) ]
+      Object o2 = toJSON body
+      in Object (o1 <> o2)
+    TemplatePdfDep body -> let
+      Object o1 = object [ "type" .= ("template_pdf" :: Text) ]
+      Object o2 = toJSON body
+      in Object (o1 <> o2)
+    OtherDep bs -> object [
+        "type" .= ("other" :: Text)
+      , "body" .= (T.decodeUtf8 . B64.encode $ bs)
+      ]
+
+-- | Description of document template
+data Template = Template {
+  -- | Template has human readable name
+    templateName          :: TemplateName
+  -- | Template expects input in YAML format
+  , templateInput         :: Maybe TemplateInput
+  -- | Template contents
+  , templateBody          :: TemplateBody
+  -- | Template dependencies (bibtex, listings, other htex files)
+  , templateDeps          :: M.Map TemplateName TemplateDependency
+  -- | Additional flags for `haskintex`
+  , templateHaskintexOpts :: [Text]
+  } deriving (Generic, Show)
+
+instance FromJSON Template where
+  parseJSON (Object o) = Template
+    <$> o .: "name"
+    <*> o .:? "input"
+    <*> o .: "body"
+    <*> o .:? "dependencies" .!= mempty
+    <*> o .:? "haskintex-opts" .!= mempty
+  parseJSON _ = mzero
+
+instance ToJSON Template where
+  toJSON Template{..} = object [
+      "name"           .= templateName
+    , "input"          .= templateInput
+    , "body"           .= templateBody
+    , "dependencies"   .= templateDeps
+    , "haskintex-opts" .= templateHaskintexOpts
+    ]
+
+-- | Same as 'TemplateDependency' but keeps contents in separate files
+data TemplateDependencyFile =
+  -- | Bibtex file for references to other documents. Need call to bibtex.
+  -- Name of dependency is a filename with contents.
+    BibtexDepFile
+  -- | HTex file that need to be compiled to .tex file
+  -- Name of dependency defines a subfolder for the template.
+  | TemplateDepFile TemplateFile
+  -- | HTex file that need to be compiled to .pdf file
+  -- Name of dependency deinfes a subfolder for the template.
+  | TemplatePdfDepFile TemplateFile
+  -- | Any other file that doesn't need a compilation (listings, images, etc)
+  -- Name of dependency is a filename with contents.
+  | OtherDepFile
+  deriving (Generic, Show)
+
+instance FromJSON TemplateDependencyFile where
+  parseJSON val@(Object o) = do
+    depType <- o .: "type"
+    case T.toLower . T.strip $ depType of
+      "bibtex"       -> pure BibtexDepFile
+      "template"     -> TemplateDepFile     <$> parseJSON val
+      "template_pdf" -> TemplatePdfDepFile  <$> parseJSON val
+      "other"        -> pure OtherDepFile
+      _ -> fail $ "Unknown template type " <> unpack depType
+  parseJSON _ = mzero
+
+instance ToJSON TemplateDependencyFile where
+  toJSON d = case d of
+    BibtexDepFile -> object [
+        "type" .= ("bibtex" :: Text)
+      ]
+    TemplateDepFile body -> let
+      Object o1 = object [ "type" .= ("template" :: Text) ]
+      Object o2 = toJSON body
+      in Object (o1 <> o2)
+    TemplatePdfDepFile body -> let
+      Object o1 = object [ "type" .= ("template_pdf" :: Text) ]
+      Object o2 = toJSON body
+      in Object (o1 <> o2)
+    OtherDepFile -> object [
+        "type" .= ("other" :: Text)
+      ]
+
+-- | Same as 'Template', but holds info about template content and dependencies
+-- in other files.
+data TemplateFile = TemplateFile {
+  -- | Template has human readable name
+    templateFileName      :: TemplateName
+  -- | Template expects input in JSON format. The field contains filename of the YAML file.
+  , templateFileInput     :: Maybe Text
+  -- | Template contents filename.
+  , templateFileBody      :: Text
+  -- | Template dependencies (bibtex, listings, other htex files)
+  , templateFileDeps      :: M.Map TemplateName TemplateDependencyFile
+  -- | Additional flags for `haskintex`
+  , templateFileHaskintexOpts :: [Text]
+  } deriving (Generic, Show)
+
+instance FromJSON TemplateFile where
+  parseJSON (Object o) = TemplateFile
+    <$> o .: "name"
+    <*> o .:? "input"
+    <*> o .: "body"
+    <*> o .:? "dependencies" .!= mempty
+    <*> o .:? "haskintex-opts" .!= mempty
+  parseJSON _ = mzero
+
+instance ToJSON TemplateFile where
+  toJSON TemplateFile{..} = object [
+      "name"           .= templateFileName
+    , "input"          .= templateFileInput
+    , "body"           .= templateFileBody
+    , "dependencies"   .= templateFileDeps
+    , "haskintex-opts" .= templateFileHaskintexOpts
+    ]
diff --git a/stack.yaml b/stack.yaml
new file mode 100644
--- /dev/null
+++ b/stack.yaml
@@ -0,0 +1,66 @@
+# This file was automatically generated by 'stack init'
+#
+# Some commonly used options have been documented as comments in this file.
+# For advanced use and comprehensive documentation of the format, please see:
+# http://docs.haskellstack.org/en/stable/yaml_configuration/
+
+# Resolver to choose a 'specific' stackage snapshot or a compiler version.
+# A snapshot resolver dictates the compiler version and the set of packages
+# to be used for project dependencies. For example:
+#
+# resolver: lts-3.5
+# resolver: nightly-2015-09-21
+# resolver: ghc-7.10.2
+# resolver: ghcjs-0.1.0_ghc-7.10.2
+# resolver:
+#  name: custom-snapshot
+#  location: "./custom-snapshot.yaml"
+resolver: lts-7.14
+
+# User packages to be built.
+# Various formats can be used as shown in the example below.
+#
+# packages:
+# - some-directory
+# - https://example.com/foo/bar/baz-0.0.2.tar.gz
+# - location:
+#    git: https://github.com/commercialhaskell/stack.git
+#    commit: e7b331f14bcffb8367cd58fbfc8b40ec7642100a
+# - location: https://github.com/commercialhaskell/stack/commit/e7b331f14bcffb8367cd58fbfc8b40ec7642100a
+#   extra-dep: true
+#  subdirs:
+#  - auto-update
+#  - wai
+#
+# A package marked 'extra-dep: true' will only be built if demanded by a
+# non-dependency (i.e. a user package), and its test suites and benchmarks
+# will not be run. This is useful for tweaking upstream packages.
+packages:
+- '.'
+# Dependency packages to be pulled from upstream that are not in the resolver
+# (e.g., acme-missiles-0.3)
+extra-deps: []
+
+# Override default flag values for local packages and extra-deps
+flags: {}
+
+# Extra package databases containing global packages
+extra-package-dbs: []
+
+# Control whether we use the GHC we find on the path
+# system-ghc: true
+#
+# Require a specific version of stack, using version ranges
+# require-stack-version: -any # Default
+# require-stack-version: ">=1.3"
+#
+# Override the architecture used by stack, especially useful on Windows
+# arch: i386
+# arch: x86_64
+#
+# Extra directories used by stack for building
+# extra-include-dirs: [/path/to/dir]
+# extra-lib-dirs: [/path/to/dir]
+#
+# Allow a newer minor version of GHC than the snapshot specifies
+# compiler-check: newer-minor
