hpastetwo

org mode data accessing

author
David Miani
age
340 days
language
haskell
  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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE NoMonomorphismRestriction #-}

import Data.Generics
import Data.Typeable

import Text.Regex.Posix
import Control.Monad.Error
import Data.Char

-- {{{ Utility types/functions
eitherOr :: Either a b -> Either a b -> Either a b
eitherOr x@(Right _) _ = x
eitherOr  _ y  = y



-- {{{ Test it

testProgram :: IO ()
testProgram = do
  putStrLn "##### using syb #####"
  putStrLn "-> test getting project 14 desc"
  print $ getP14Desc test2
  putStrLn "-> test adding a tag"
  print $ addHardTag test2
  putStrLn "##### using multiconAccessor code #####"
  putStrLn "-> test getting project 14 desc"
  print $ getP14Desc2 test2
  putStrLn "-> test adding a tag"
  print $ addHardTag2 test2
  

-- {{{ Using scrap your boilerplate

-- get all headings
getHeadings = everything (++) ([] `mkQ` matchHeading) where
    matchHeading h@(Heading {}) = [h]
    matchHeading _ = []

-- add a row to every table
addToTables = everywhere (mkT addNewRow) where
    addNewRow (Table pos rows) = Table pos (RowSep pos : rows)
    addNewRow x = x


-- find project14, then get description
getP14Desc :: OrgElement -> Either ErrString String
getP14Desc org = everything eitherOr (Left descError `mkQ` findDesc) =<<
                 everything eitherOr (Left findError `mkQ` findP14) org
    where
      findP14 h@(Heading {headingName=name})
          | name == "Project14" = Right h
      findP14 _ = Left findError

      findDesc (Paragraph {paragraphText=text})
          | text =~ "Description" = Right text
      findDesc _ = Left findError

      descError = "Couldn't find description for project"
      findError = "Couldn't find project."
      
addHardTag org = everywhere (mkT addTagToP2) org where
    addTagToP2 h@(Heading {headingName=name}) 
               | name == "Project 2" = everywhere (mkT addTag) h
    addTagToP2 x = x
    addTag text 
           | text =~ "Tags:" = text ++ ",newtag"
    addTag x = x



-- {{{ Using my MulticonAccessor code


projectAccessor name = headingChildren' `chain` -- top level elements
                       headingChildren' `chain` -- level 2 
                       liftFilterS (== name) headingName'



getP14Desc2 = getVal $
              projectAccessor "Project14" `chain`
              headingChildren' `chain`
              liftFilterS (=~ "Description:") paragraphText' `chain`
              paragraphText'

addHardTag2 = modVal
              (projectAccessor "Project 2" `chain`
               headingChildren' `chain`
               liftFilterS (=~ "Tags:") paragraphText' `chain`
               paragraphText')
              (++ ",newtag")
             



-- {{{ Accessor core functions

-- r is the type of the field being accessed, d is the type on the main data type
newtype MultiConAccessor d f = MultiConAccessor ((d -> [f]),((f -> f) -> d -> d))

multiConAccessor :: (d -> [f]) -> ((f -> f) -> d -> d) -> MultiConAccessor d f
multiConAccessor getter setter = MultiConAccessor (getter,setter)


getVal :: MultiConAccessor d f -> d -> [f]
getVal (MultiConAccessor (getF,_)) value = getF value
    



setVal :: MultiConAccessor d f -> f -> d -> d
setVal (MultiConAccessor (_,setF)) v r = setF (const v) r

modVal :: MultiConAccessor d f -> (f -> f) -> d -> d
modVal (MultiConAccessor (getF,setF)) f value = setF f value
                                               
chain :: MultiConAccessor d1 f1 -> MultiConAccessor f1 f2 -> MultiConAccessor d1 f2
(MultiConAccessor (getF1, setF1)) `chain` (MultiConAccessor (getF2, setF2))
    = multiConAccessor getNew setNew where
      getNew value = getF1 value >>= getF2
      setNew f = setF1 (setF2 f)
      -- setNew newValue record = setF1 (setF2 newValue) record





-- | Modifies a MultiConAccessor to only operate for elements that
-- | pass the filter function
liftFilter :: ([f] -> Bool) -> MultiConAccessor d f -> MultiConAccessor d d
liftFilter filterF (MultiConAccessor (getF,modF)) = multiConAccessor getH modH where
    -- getH :: d -> [d]
    getH x = if filterF (getF x) then [x] else []
    modH f d = if filterF (getF d) then f d else d
    -- modH f d = modF (\v -> if filterF v then f v else v) d 

-- | uses f instead of [f] for the type for the filter func
liftFilterS :: (f -> Bool) -> MultiConAccessor d f -> MultiConAccessor d d
liftFilterS f = liftFilter fNew where
    fNew [] = False
    fNew (x:_) = f x
      

           
    
    


-- {{{ Data definitions

data FilePos = FilePos Int
                deriving (Show,Read,Eq,Data,Typeable)

type ErrString = String

class HasPosition a where
    getPos :: a -> FilePos

instance HasPosition OrgElement where
    getPos = elemPosition

instance HasPosition OrgRow where
    getPos = rowPosition

data OrgElement = Heading
    {
      elemPosition :: FilePos,
      headingLevel :: Integer,
      headingName :: String,
      headingChildren :: [OrgElement]
    }
                | Paragraph
    {
      elemPosition :: FilePos,
      paragraphText :: String
    }
                | Table
    {
      elemPosition :: FilePos,
      tableRows :: [OrgRow]
    }
                deriving (Show,Read,Eq,Data,Typeable)


data OrgRow = RowSep  { rowPosition :: FilePos}
            | Row { rowPosition :: FilePos, rows :: [String]}
              deriving (Show,Read,Eq,Data,Typeable)




-- {{{ org accessor definitions (this should be automatically generated)
headingLevel' :: MultiConAccessor OrgElement Integer
headingLevel' = multiConAccessor getH modH where
    getH (Heading {headingLevel=level}) = [level]
    getH _ = []
    modH f h@(Heading {headingLevel=level}) =
        h {headingLevel=f level}
    modH _ x = x


headingName' :: MultiConAccessor OrgElement String
headingName' = multiConAccessor getH modH where
    getH (Heading {headingName=name}) = [name]
    getH _ = []
    modH f h@(Heading {headingName=name}) =
        h {headingName=f name}
    modH _ x = x

headingChildrenRaw' :: MultiConAccessor OrgElement [OrgElement]
headingChildrenRaw' = multiConAccessor getH modH where
    getH (Heading {headingChildren=children}) = [children]
    getH _ = []
    modH f h@(Heading {headingChildren=children}) =
        h {headingChildren=f children}
    modH _ x = x

headingChildren' :: MultiConAccessor OrgElement OrgElement
headingChildren' = multiConAccessor getH modH where
    getH (Heading {headingChildren=children}) = children
    getH _ = []
    modH f h@(Heading {headingChildren=children}) =
        h {headingChildren=fmap f children}
    modH _ x = x



paragraphText' :: MultiConAccessor OrgElement String
paragraphText' = multiConAccessor getH modH where
    getH (Paragraph {paragraphText=text}) = [text]
    getH _ = []
    modH f h@(Paragraph {paragraphText=text}) =
        h {paragraphText=f text}
    modH _ x = x-- wrapMaybe :: (Typeable a, Typeable b) => a -> Maybe b

tableRows' :: MultiConAccessor OrgElement [OrgRow]
tableRows' = multiConAccessor getH modH where
    getH (Table {tableRows=rows}) = [rows]
    getH _ = []
    modH f h@(Table {tableRows=rows}) =
        h {tableRows=f rows}
    modH _ x = x


paragraph' :: MultiConAccessor OrgElement OrgElement
paragraph' = multiConAccessor getH modH where
    getH p@(Paragraph {}) = [p]
    getH _ = []
    modH f p@(Paragraph {}) = f p
    modH _ x = x

heading' :: MultiConAccessor OrgElement OrgElement
heading' = multiConAccessor getH modH where
    getH p@(Heading {}) = [p]
    getH _ = []
    modH f p@(Heading {}) = f p
    modH _ x = x





-- {{{ Test data (test2 refers to the example in the email)


nullP = FilePos 0
-- test = Heading 0 "file.org" [ObjWithPos nullP (Heading 1 "h1" [ObjWithPos nullP (Table [ObjWithPos nullP (Row ["a", "b", "c"]), ObjWithPos nullP RowSep, ObjWithPos nullP (Row ["1", "2", "3"])]), ObjWithPos nullP (Paragraph "hello")]), ObjWithPos nullP (Heading 1 "another heading" [])]
test = Heading nullP 0 "file.org" [  (Heading nullP 1 "h1" [  (Table nullP [  (Row nullP ["a", "b", "c"]),   RowSep nullP,   (Row nullP ["1", "2", "3"])]),   (Paragraph nullP "hello")]),   (Heading nullP 1 "another heading" [])]

test2 = Heading nullP 0 "file.org" [  (Heading nullP 1 "2007 Projects" [  (Heading nullP 2 "Project 1" [  (Paragraph nullP "Description: 1"),   (Paragraph nullP "Tags: None")]),   (Heading nullP 2 "Project 2" [  (Paragraph nullP "Tags: asdf,fdsa"),  (Paragraph nullP "Description: hello")])]),  (Heading nullP 1 "2009 Projects" [  (Heading nullP 2 "Project14" [  (Paragraph nullP "Tags: RightProject"),  (Paragraph nullP "Description: we want this")])])]