Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 41 additions & 31 deletions Data/Graph/Analysis/Reporting/Pandoc.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
{-# LANGUAGE OverloadedStrings #-}

{- |
Module : Data.Graph.Analysis.Reporting.Pandoc
Description : Graphalyze Types and Classes
Expand Down Expand Up @@ -39,6 +41,9 @@ import Data.List (intersperse)
import Data.Maybe (fromJust, isNothing)
import System.Directory (removeDirectoryRecursive)
import System.FilePath ((<.>), (</>))
import Data.Text (Text)
import qualified Data.Text as Text
import qualified Data.Text.IO as Text

-- -----------------------------------------------------------------------------

Expand All @@ -47,7 +52,7 @@ import System.FilePath ((<.>), (</>))
-}

pandocHtml :: PandocDocument
pandocHtml = pd { writer = writeHtmlString
pandocHtml = pd { writer = writeHtml5String
, extension = "html"
, templateName = "html"
, extGraphProps = Just VProps { grSize = DefaultSize
Expand Down Expand Up @@ -84,11 +89,11 @@ pandocMarkdown = pd { writer = writeMarkdown
-- | Definition of a Pandoc Document. Size measurements are in inches,
-- and a 6:4 ratio is used for width:length.
data PandocDocument = PD { -- | The Pandoc document style
writer :: WriterOptions -> Pandoc -> String
writer :: WriterOptions -> Pandoc -> PandocPure Text
-- | The file extension used
, extension :: FilePath
-- | Which template to get.
, templateName :: String
, templateName :: Text
-- | Size of graphs to be produced.
, graphProps :: VisProperties
-- | Optional size of external linked graphs.
Expand Down Expand Up @@ -144,27 +149,32 @@ defaultProcess = PP { secLevel = 1

-- | Create the document.
createPandoc :: PandocDocument -> Document -> IO (Maybe FilePath)
createPandoc p d = do created <- tryCreateDirectory dir
-- If the first one fails, so will this one.
_ <- tryCreateDirectory $ dir </> gdir
if not created
then failDoc
else do d' <- addLegend dir gdir (graphProps p) d
elems <- multiElems pp $ content d'
case elems of
Just es -> do let es' = htmlAuthDt : es
pnd = Pandoc meta es'
doc = convert pnd
wr <- tryWrite doc
case wr of
(Right _) -> success
(Left _) -> failDoc
Nothing -> failDoc
where
createPandoc p d = do
created <- tryCreateDirectory dir
-- If the first one fails, so will this one.
_ <- tryCreateDirectory $ dir </> gdir
if not created
then failDoc
else do
d' <- addLegend dir gdir (graphProps p) d
elems <- multiElems pp $ content d'
case elems of
Just es -> do
let es' = htmlAuthDt : es
pnd = Pandoc meta es'
case runPure $ convert pnd of
Left _ -> failDoc
Right text -> do
doc <- tryWrite text
case doc of
(Right _) -> success
(Left _) -> failDoc
Nothing -> failDoc
where
dir = rootDirectory d
gdir = graphDirectory d
auth = author d
dt = date d
auth = (Text.pack . author) d
dt = (Text.pack . date) d
meta = makeMeta (title d) auth dt
-- Html output doesn't show date and auth anywhere by default.
htmlAuthDt = htmlInfo auth dt
Expand All @@ -177,8 +187,8 @@ createPandoc p d = do created <- tryCreateDirectory dir
}
convert = writer p writerOptions
file = dir </> fileFront d <.> extension p
tryWrite :: String -> IO (Either SomeException ())
tryWrite = try . writeFile file
tryWrite :: Text -> IO (Either SomeException ())
tryWrite = try . Text.writeFile file
success = return (Just file)
failDoc = removeDirectoryRecursive dir >> return Nothing

Expand All @@ -189,25 +199,25 @@ createPandoc p d = do created <- tryCreateDirectory dir
-}

-- | The meta information
makeMeta :: DocInline -> String -> String -> Meta
makeMeta :: DocInline -> Text -> Text -> Meta
makeMeta tle a t = P.makeMeta (inlines tle) [[Str a]] [Str t]

-- | Html output doesn't show the author and date; use this to print it.
htmlInfo :: String -> String -> Block
htmlInfo :: Text -> Text -> Block
htmlInfo auth dt = RawBlock (Format "html") html
where
heading = "<h1>Document Information</h1>"
html = unlines [heading, htmlize auth, htmlize dt]
htmlize str = "<blockquote><p><em>" ++ str ++ "</em></p></blockquote>"
html = Text.unlines [heading, htmlize auth, htmlize dt]
htmlize str = "<blockquote><p><em>" <> str <> "</em></p></blockquote>"

-- | Link conversion
loc2target :: Location -> Target
loc2target (Web url) = (url,"")
loc2target (File file) = (file,"")
loc2target (Web url) = (Text.pack url,"")
loc2target (File file) = (Text.pack file,"")

-- | Conversion of simple inline elements.
inlines :: DocInline -> [Inline]
inlines (Text str) = [Str str]
inlines (Text str) = [Str (Text.pack str)]
inlines BlankSpace = [Space]
inlines (Grouping grp) = concat . intersperse [Space] $ map inlines grp
inlines (Bold inl) = [Strong (inlines inl)]
Expand Down
115 changes: 50 additions & 65 deletions Graphalyze.cabal
Original file line number Diff line number Diff line change
@@ -1,65 +1,50 @@
Name: Graphalyze
Version: 0.15.0.0
Synopsis: Graph-Theoretic Analysis library.
Description: A library to use graph theory to analyse the relationships
inherent in discrete data.
Category: Graphs, Algorithms
License: OtherLicense
License-File: LICENSE
Copyright: (c) Ivan Lazar Miljenovic
Author: Ivan Lazar Miljenovic
Maintainer: Ivan.Miljenovic@gmail.com
Extra-Source-Files: TODO
Cabal-Version: >= 1.6
Build-Type: Simple

Tested-With: GHC == 7.4.2, GHC == 7.6.3, GHC == 7.8.4,
GHC == 7.10.2, GHC == 7.11.*

Source-Repository head
Type: git
Location: https://github.com/ivan-m/Graphalyze.git

Flag old-locale {
Description: Use old-locale and time < 1.5
Manual: False
Default: False
}

Library {
Build-Depends: base == 4.*,
array,
containers,
directory,
filepath,
process,
random,
bktrees >= 0.2 && <0.4,
fgl == 5.5.*,
graphviz >= 2999.15 && < 2999.20,
pandoc == 1.19.*,
text

if flag(old-locale) {
Build-Depends: time < 1.5,
old-locale
} else {
Build-Depends: time >= 1.5 && < 1.9
}

Exposed-Modules: Data.Graph.Analysis
Data.Graph.Analysis.Types
Data.Graph.Analysis.Utils
Data.Graph.Analysis.Visualisation
Data.Graph.Analysis.Algorithms
Data.Graph.Analysis.Algorithms.Common
Data.Graph.Analysis.Algorithms.Directed
Data.Graph.Analysis.Algorithms.Clustering
Data.Graph.Analysis.Reporting
Data.Graph.Analysis.Reporting.Pandoc

Other-Modules: Data.Graph.Analysis.Internal,
Paths_Graphalyze

Ghc-Options: -Wall
}
cabal-version: 2.2
name: Graphalyze
version: 0.16.0.0
author: Ivan Lazar Miljenovic
maintainer: Ivan.Miljenovic@gmail.com
synopsis: Graph-Theoretic Analysis library.
description:
A library to use graph theory to analyse the relationships
inherent in discrete data.
category: Graphs, Algorithms
license-file: LICENSE
tested-with: GHC == 8.4.2
extra-source-files: TODO
build-type: Simple
copyright: (c) Ivan Lazar Miljenovic

library
exposed-modules:
Data.Graph.Analysis
Data.Graph.Analysis.Algorithms
Data.Graph.Analysis.Algorithms.Clustering
Data.Graph.Analysis.Algorithms.Common
Data.Graph.Analysis.Algorithms.Directed
Data.Graph.Analysis.Reporting
Data.Graph.Analysis.Reporting.Pandoc
Data.Graph.Analysis.Types
Data.Graph.Analysis.Utils
Data.Graph.Analysis.Visualisation
build-depends:
, array
, base == 4.*
, bktrees >= 0.2
, containers
, directory
, fgl >= 5.6
, filepath
, graphviz >= 2999.20
, pandoc >= 2.0
, process
, random
, text
, time
ghc-options: -Wall
other-modules:
Data.Graph.Analysis.Internal,
Paths_Graphalyze

source-repository head
type: git
location: https://github.com/ivan-m/Graphalyze.git