packages feed

authoring (empty) → 0.1.0.0

raw patch · 12 files changed

+366/−0 lines, 12 filesdep +HaTeXdep +basedep +citation-resolvesetup-changed

Dependencies added: HaTeX, base, citation-resolve, containers, lens, mtl, safe, text, transformers

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2013, Takayuki Muranushi++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 Takayuki Muranushi 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ authoring.cabal view
@@ -0,0 +1,47 @@+-- Initial authoring.cabal generated by cabal init.  For further +-- documentation, see http://haskell.org/cabal/users-guide/++name:                authoring+version:             0.1.0.0+synopsis:            A library for writing papers+description:         This package is parser combinator library for writing papers.+homepage:            http://github.com/nushio3/authoring+bug-reports:         http://github.com/nushio3/authoring/issues+license:             BSD3+license-file:        LICENSE+author:              Takayuki Muranushi+maintainer:          muranushi@gmail.com+copyright:           (C) Takayuki Muranushi, 2013+category:            Text+build-type:          Simple+cabal-version:       >=1.8++source-repository head+  type: git+  location: git://github.com/nushio3/authoring.git++library+  hs-source-dirs:    src/+  exposed-modules:     +    Text.Authoring.Bibliography+    Text.Authoring.Combinator+    Text.Authoring.Combinator.Cite+    Text.Authoring.Combinator.Meta+    Text.Authoring.Combinator.Ref+    Text.Authoring.Combinator.Writer+    Text.Authoring.Document+    Text.Authoring.Label+    Text.Authoring.State+  -- other-modules:       +  build-depends:+      base ==4.*+    , citation-resolve >= 0.4.2+    , containers >= 0.5+    , HaTeX >= 3.8+    , lens+    , mtl+    , safe+    , text+    , transformers++  
+ src/Text/Authoring/Bibliography.hs view
@@ -0,0 +1,20 @@+module Text.Authoring.Bibliography where++import           Control.Lens (use)+import           Control.Monad+import           Control.Monad.State+import           Control.Monad.IO.Class+import qualified Data.Set as Set+import qualified Data.Text as Text+import           Text.CSL.Input.Identifier (toBibTeXItem, HasDatabase)++import Text.Authoring.State++-- | generate the content for the bibliography file.++bibliographyContent :: (MonadState s m, HasAuthorState s, HasDatabase s, MonadIO m) => m Text.Text+bibliographyContent = do+  urls <- use citedUrlSet+  items <- mapM toBibTeXItem $ Set.toList urls+  return $ Text.unlines $ items+    
+ src/Text/Authoring/Combinator.hs view
@@ -0,0 +1,14 @@+module Text.Authoring.Combinator (+  +module Text.Authoring.Combinator.Cite,+module Text.Authoring.Combinator.Meta,+module Text.Authoring.Combinator.Ref,+module Text.Authoring.Combinator.Writer+                                 +                                 +                                 ) where++import Text.Authoring.Combinator.Cite+import Text.Authoring.Combinator.Meta+import Text.Authoring.Combinator.Ref+import Text.Authoring.Combinator.Writer
+ src/Text/Authoring/Combinator/Cite.hs view
@@ -0,0 +1,39 @@+{-# LANGUAGE OverloadedStrings #-}++module Text.Authoring.Combinator.Cite where++import Control.Lens+import Control.Monad+import Control.Monad.State+import Control.Monad.Writer+import qualified Data.Set as Set+import qualified Data.Text as Text+import Text.CSL.Input.Identifier (resolve, HasDatabase)+++import Text.Authoring.Combinator.Meta+import Text.Authoring.Combinator.Writer+import Text.Authoring.Document+import Text.Authoring.State+++citet, citep :: (MonadState s m, HasAuthorState s, HasDatabase s, +          MonadWriter w m, HasDocument w, MonadIO m) => [String] -> m ()++citet = citationGen "citet"+citep = citationGen "citep"++citet1, citep1 :: (MonadState s m, HasAuthorState s, HasDatabase s, +          MonadWriter w m, HasDocument w, MonadIO m) => String -> m ()++citet1 = citationGen "citet" . (:[]) -- + ---<===   I am a long man lying+citep1 = citationGen "citep" . (:[]) -- + ---<===   We are long men lying++-- | make a citation to a document(s).+citationGen :: (MonadState s m, HasAuthorState s, HasDatabase s, +          MonadWriter w m, HasDocument w, MonadIO m) => Text.Text -> [String] -> m ()+citationGen cmdName  urls = do+  forM_ urls $ \url -> do+    _ <- resolve url+    citedUrlSet %= Set.insert url +  command1 cmdName $ braces $ raw $ Text.intercalate "," $ map Text.pack urls
+ src/Text/Authoring/Combinator/Meta.hs view
@@ -0,0 +1,43 @@+{-# LANGUAGE OverloadedStrings #-}++module Text.Authoring.Combinator.Meta where++import Control.Monad.Writer+import Data.Text (Text)++import Text.Authoring.Document+import Text.Authoring.Combinator.Writer+++-- | wrap the argument @x@ using curly braces "{x}"++braces :: (MonadWriter t m, HasDocument t) => m () -> m ()+braces con = do+  raw "{"+  con+  raw "}"++  +-- | command1 x con = "\x{con}"++command1 :: (MonadWriter t m, HasDocument t) =>  Text -> m () -> m ()+command1 x con = do+  raw "\\"  +  raw x+  raw "{"+  con+  raw "}"+       +    +-- | environment x con = "\begin{x}con\end{x}"    +  +environment :: (MonadWriter t m, HasDocument t) =>  Text -> m () -> m ()+environment x con = do+  raw "\\begin{"  +  raw x+  raw "}\n"  +  con+  raw "\\end{"  +  raw x+  raw "}\n"  +
+ src/Text/Authoring/Combinator/Ref.hs view
@@ -0,0 +1,56 @@+{-# LANGUAGE OverloadedStrings #-}++module Text.Authoring.Combinator.Ref where++import           Control.Lens+import           Control.Monad.State+import           Control.Monad.Writer+import           Data.Char (isAlphaNum)+import qualified Data.Map as Map+import qualified Data.Set as Set+import           Data.Text (Text, pack)++import Text.Authoring.Document+import Text.Authoring.State+import Text.Authoring.Label+import Text.Authoring.Combinator.Writer+import Text.Authoring.Combinator.Meta++-- | refer to a label.  +ref :: (MonadState s m, HasAuthorState s, MonadWriter w m, HasDocument w) => Label -> m ()+ref lab = do+  ret <- getReference lab+  command1 "ref" $ raw ret++-- | insert a label.+label :: (MonadState s m, HasAuthorState s, MonadWriter w m, HasDocument w) => Label -> m ()+label lab = do+  ret <- getReference lab+  command1 "label" $ raw ret++-- | an interface for mapping any Label to a unique, LaTeX-safe text.++getReference :: (MonadState s m, HasAuthorState s) => Label -> m Text+getReference lab = do+  map0 <- use labelMap+  case Map.lookup lab map0 of+    Just str -> return str+    Nothing  -> do+      let cands :: [String]+          cands = cand0 : [s ++ [c]| s <- cands, c <- ['0'..'9']]+          cand0 = map modify $ show lab++          modify :: Char -> Char+          modify c+            | isAlphaNum c = c+            | otherwise    = '.'+                             +          takens :: Set.Set Text+          takens = Set.fromList $ Map.elems map0+          +          isTaken x = Set.member (pack x) takens++          freeStr :: Text+          freeStr = pack $ head $ filter (not . isTaken) cands+      labelMap %= (Map.insert lab freeStr)+      return freeStr
+ src/Text/Authoring/Combinator/Writer.hs view
@@ -0,0 +1,37 @@+{-# LANGUAGE MultiParamTypeClasses #-}++-- | Names of this module are intentitionally kept too short.+--   Use this module with quantifier.++module Text.Authoring.Combinator.Writer where++import Control.Lens (scribe)+import Control.Monad.Writer+import Data.Text (Text, pack)+import Prelude +import Safe (readMay)+import Text.LaTeX.Base.Syntax (LaTeX)+import qualified Text.LaTeX.Base.Commands as LTX +import qualified Text.LaTeX.Base.Render as LTX +import qualified Text.LaTeX.Base.Syntax as LTX +import qualified Text.LaTeX.Base.Texy as LTX ++import Text.Authoring.Document++-- | Scribe a LaTeX syntax into the target document.++latex :: (MonadWriter t m, HasDocument t) => LaTeX -> m ()+latex x = scribe latexSrc x++-- | Scribe a raw text (unescaped) into the target.++raw :: (MonadWriter t m, HasDocument t) => Text -> m ()+raw x = latex $ LTX.raw x+++-- | Shows objects of type 'a' using Show interface+--   Escapes LaTeX special characters.++esc :: (MonadWriter t m, HasDocument t, Show a) => a -> m ()+esc x = raw $ LTX.protectText $ pack $ Prelude.show x+
+ src/Text/Authoring/Document.hs view
@@ -0,0 +1,16 @@+{-# LANGUAGE GeneralizedNewtypeDeriving #-}+{-# LANGUAGE StandaloneDeriving #-}+{-# LANGUAGE TemplateHaskell #-}++module Text.Authoring.Document where++import Control.Lens.TH+import Data.Monoid+import Text.LaTeX.Base.Syntax (LaTeX)++newtype Document = Document { _latexSrc :: LaTeX }+  deriving (Eq, Show)++deriving instance Monoid Document++makeClassy ''Document
+ src/Text/Authoring/Label.hs view
@@ -0,0 +1,38 @@+module Text.Authoring.Label +       ( Label(..),(<>),(./))+       where++import           Data.Monoid+import           Data.String (IsString(..))+import           Data.Typeable+import           Data.Text (pack)+import           Text.LaTeX (raw)++-- | 'Label's are used to create a unique reference label+--   within a paper.+data Label +  = FromType !TypeRep +  | FromString !String+  | Ap !Label !Label+  | Empty+  deriving (Eq, Ord)++instance Monoid Label where+  mempty = Empty+  mappend Empty x = x+  mappend x Empty = x+  mappend x y = Ap x y++instance IsString Label where+  fromString = FromString++instance Show Label where+  show (FromType r) = show r+  show (FromString s) = s+  show (Ap a b) = show a ++ "." ++ show b++infixl 1 ./++-- | Create a slightly different version of the label.+(./) :: Show a => Label -> a -> Label+l ./ x = Ap l (FromString $ show x)
+ src/Text/Authoring/State.hs view
@@ -0,0 +1,24 @@+{-# LANGUAGE TemplateHaskell #-}++module Text.Authoring.State where++import           Control.Lens+import           Control.Lens.TH+import qualified Data.Map as Map+import qualified Data.Set as Set+import           Data.Text (Text)+import qualified Text.CSL.Input.Identifier.Internal as Citation++import           Text.Authoring.Label++data AuthorState+  = AuthorState+  { _labelMap :: Map.Map Label Text+  , _citationDB :: Citation.Database+  , _citedUrlSet :: Set.Set String+  }++makeClassy ''AuthorState++instance Citation.HasDatabase AuthorState where+  database = citationDB