summaryrefslogtreecommitdiff
path: root/indexing/hsindex/src/HsIndex/Files.hs
blob: 5f1d15c3ea54017c5249570c56951476583260d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
--
-- GNU General Public License (GPLv3)
--
-- THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) “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 AUTHOR(S) 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.

-- |
-- Module      :  HsIndex.Files
-- Copyright   :  Jean-Luc JOULIN 2018-2019
-- License     :  General Public Licence (GPLv3)
-- Maintainer  :  Jean-Luc JOULIN  <jean-luc-joulin@orange.fr>
-- Stability   :  alpha
-- Portability :  portable
-- Module for reading input and style files.
--
-- This module also provide the function to write output file.
--




module HsIndex.Files
    (
    -- * Read functions
      readIndexFile
    , readStyleFile
    , readDefinitionFile
    , readAllFile

    -- * Writing function
    , writeIndex
    )
    where

import           Data.Char
import           HsIndex.Functions
import           HsIndex.Show
import           HsIndex.Parser
import           HsIndex.Types
import           Text.Parsec
import           Text.Parsec.String
import           System.Directory               ( doesFileExist )
import qualified Data.Text                     as T
import qualified Data.Text.IO                  as T
import           Data.Functor.Identity


-- | Read an index input file.
readIndexFile :: FilePath                     -- ^ The path to the index file.
              -> ParsecT String () Identity t -- ^ The parser to apply on the file to extract datas.
              -> (t -> IO ())                 -- ^ List of operations to perform on parsed datas.
              -> IO ()
readIndexFile fidx parsfun fun = do
    e <- doesFileExist fidx
    if e
        then do
            f <- readFile fidx
            case parse parsfun "(stdin)" f of
                Left  err  -> do
                    putStrLn $ "/!\\ ERROR reading Index file : " ++ fidx
                    print err
                Right resu -> fun resu
        else
            putStrLn
            $  "/!\\ ERROR the Index file : "
            ++ fidx
            ++ " does not exist !\n"


-- | Read a style input file
--
-- If no path is provided for the style file, the standard style 'styleBasic' is applied.
--
readStyleFile :: IndexType             -- ^ The type of style.
              -> (IndexStyle -> IO ()) -- ^ Operations to apply with the parsed style.
              -> IO ()
readStyleFile StyleBasic         fun = do
    putStrLn "Using the default basic style"
    fun styleBasic

readStyleFile StyleDouble        fun = do
    putStrLn "Using the default double letters style"
    fun styleDoubleHeading



readStyleFile (Stylecustom fsty) fun = do
    e <- doesFileExist fsty
    if e
        then do
            f <- readFile fsty
            case parse (parseStyleFile styleBasic) "(stdin)" f of
                Left  err  -> do
                    putStrLn $ "/!\\ ERROR reading Style file : " ++ fsty
                    print err
                Right resu -> fun resu
        else
            putStrLn
            $  "/!\\ ERROR the Style file : "
            ++ fsty
            ++ " does not exist !\n"


-- | Read a language definition input file.
readDefinitionFile :: FilePath           -- ^ The path to the definition file.
                   -> (LangDef -> IO ()) -- ^ Operations to apply with the parsed style.
                   -> IO ()
readDefinitionFile fdef fun = do
    e <- doesFileExist fdef
    if e
        then do
            f <- readFile fdef
            case runParser (parseLanguageFile) emptyPermState "(stdin)" f of
                Left  err  -> do
                    putStrLn $ "/!\\ ERROR reading Definition file : " ++ fdef
                    print err
                Right resu -> fun resu
        else
            putStrLn
            $  "/!\\ ERROR the Definition file : "
            ++ fdef
            ++ " does not exist !\n"


-- | Read both an index input file and style file.
--
-- If no path is provided for the style file, the standard style 'styleBasic' is applied.
--
readAllFile :: FilePath                     -- ^ The path to the index file.
            -> IndexType                    -- ^ The type of index.
            -> ParsecT String () Identity t -- ^ The parser to apply on the index file.
            -> (t -> IndexStyle -> IO ())   -- ^ Operations to apply with the parsed index data and style.
            -> IO ()
readAllFile fidx mbfsty parsfun fun =
    readIndexFile fidx parsfun (readStyleFile mbfsty . fun)


-- | Write the sorted entries in a output file with the format provided in the `IndexStyle`.
writeIndex :: FilePath   -- ^ The path to the output file
           -> IndexStyle -- ^ The style file.
           -> Bool       -- ^ Automatic conversion to pages ranges
           -> Index      -- ^ The index.
           -> IO ()
writeIndex file style rng sec = do
    putStrLn $ "Writing index to File : " ++ file
    T.writeFile file $ showIndex style rng sec