Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge pull request #11 from akaspin/master

Inverted sections with missing context
  • Loading branch information...
commit 27ddb46e1130ce0c43cde5a9f380ada4af9f3be0 2 parents a72b0ed + 3d80fcd
Sergey Lymar authored February 02, 2012
86  Text/Hastache.hs
@@ -103,7 +103,7 @@ import qualified Data.Text as Text
103 103
 import qualified Data.Text.Lazy as LText
104 104
 
105 105
 (~>) :: a -> (a -> b) -> b
106  
-x ~> f = f $ x
  106
+x ~> f = f x
107 107
 infixl 9 ~>
108 108
 
109 109
 -- | Data for Hastache variable
@@ -146,10 +146,10 @@ instance MuVar Char where
146 146
     toLByteString a = (a : "") ~> encodeStr ~> toLBS
147 147
 
148 148
 instance MuVar a => MuVar [a] where
149  
-    toLByteString a = (toLByteString '[') <+> cnvLst <+> (toLByteString ']')
  149
+    toLByteString a = toLByteString '[' <+> cnvLst <+> toLByteString ']'
150 150
         where
151  
-        cnvLst = (map toLByteString a) ~> 
152  
-                (LZ.intercalate (toLByteString ','))
  151
+        cnvLst = map toLByteString a ~> 
  152
+                LZ.intercalate (toLByteString ',')
153 153
         (<+>) = LZ.append
154 154
 
155 155
 instance MuVar [Char] where
@@ -214,9 +214,9 @@ htmlEscape str = LZ.unpack str ~> proc ~> LZ.pack
214 214
         | h == ord8 '\''= stp "&#39;" t
215 215
         | h == ord8 '<' = stp "&lt;" t
216 216
         | h == ord8 '>' = stp "&gt;" t
217  
-        | otherwise     = h : (proc t)
  217
+        | otherwise     = h : proc t
218 218
     proc [] = []
219  
-    stp a t = (map ord8 a) ++ (proc t)
  219
+    stp a t = map ord8 a ++ proc t
220 220
 
221 221
 -- | No escape
222 222
 emptyEscape :: LZ.ByteString -> LZ.ByteString
@@ -241,7 +241,7 @@ findBlock ::
241 241
     -> ByteString
242 242
     -> Maybe (ByteString, Word8, ByteString, ByteString)
243 243
 findBlock str otag ctag = do
244  
-    guard (length fnd > (length otag))
  244
+    guard (length fnd > length otag)
245 245
     Just (pre, symb, inTag, afterClose)
246 246
     where
247 247
     (pre, fnd) = breakSubstring otag str
@@ -279,7 +279,7 @@ findCloseSection str name otag ctag = do
279 279
     (before, after) = breakSubstring close str
280 280
 
281 281
 trimCharsTest :: Word8 -> Bool
282  
-trimCharsTest = (`elem` (encodeStr " \t"))
  282
+trimCharsTest = (`elem` encodeStr " \t")
283 283
 
284 284
 trimAll :: ByteString -> ByteString
285 285
 trimAll str = span trimCharsTest str ~> snd ~> spanEnd trimCharsTest ~> fst
@@ -305,7 +305,7 @@ processBlock :: MonadIO m =>
305 305
     -> ByteString 
306 306
     -> MuConfig 
307 307
     -> ReaderT (IORef BSB.Builder) m ()
308  
-processBlock str contexts otag ctag conf = do
  308
+processBlock str contexts otag ctag conf = 
309 309
     case findBlock str otag ctag of
310 310
         Just (pre, symb, inTag, afterClose) -> do
311 311
             addResBS pre
@@ -331,15 +331,14 @@ renderBlock contexts symb inTag afterClose otag ctag conf
331 331
     | symb == ord8 '&' || (symb == ord8 '{' && otag == defOTag) = do
332 332
         readVar contexts (tail inTag ~> trimAll) ~> addResLZ
333 333
         next afterClose
334  
-    -- section. inverted section
  334
+    -- section, inverted section
335 335
     | symb == ord8 '#' || symb == ord8 '^' = 
336  
-        let normalSection = symb == ord8 '#' in do
337 336
         case findCloseSection afterClose (tail inTag) otag ctag of
338 337
             Nothing -> next afterClose
339 338
             Just (sectionContent', afterSection') -> 
340 339
                 let 
341 340
                     dropNL str = 
342  
-                        if (length str) > 0 && (head str) == ord8 '\n' 
  341
+                        if length str > 0 && head str == ord8 '\n' 
343 342
                            then tail str
344 343
                            else str
345 344
                     sectionContent = dropNL sectionContent'
@@ -350,44 +349,40 @@ renderBlock contexts symb inTag afterClose otag ctag conf
350 349
                     tlInTag = tail inTag
351 350
                     readContext = map ($ tlInTag) contexts 
352 351
                         ~> List.find (not . isMuNothing)
353  
-                in do
354  
-                case readContext of
355  
-                    Just (MuList []) -> 
356  
-                        if normalSection then do next afterSection
357  
-                        else do
358  
-                            processBlock sectionContent
359  
-                                contexts otag ctag conf
360  
-                            next afterSection
361  
-                    Just (MuList b) -> 
362  
-                        if normalSection then do
  352
+                in 
  353
+                if symb == ord8 '#'
  354
+                    then case readContext of
  355
+                        Just (MuList []) -> next afterSection
  356
+                        Just (MuList b) -> do
363 357
                             mapM_ (\c -> processBlock sectionContent
364 358
                                 (c:contexts) otag ctag conf) b
365 359
                             next afterSection
366  
-                        else do next afterSection
367  
-                    Just (MuBool True) -> 
368  
-                        if normalSection then do
  360
+                        Just (MuBool True) -> do 
369 361
                             processBlock sectionContent
370 362
                                 contexts otag ctag conf
371 363
                             next afterSection
372  
-                        else do next afterSection
373  
-                    Just (MuBool False) -> 
374  
-                        if normalSection then do next afterSection
375  
-                        else do
376  
-                            processBlock sectionContent
377  
-                                contexts otag ctag conf
378  
-                            next afterSection
379  
-                    Just (MuLambda func) -> 
380  
-                        if normalSection then do
  364
+                        Just (MuLambda func) -> do
381 365
                             func sectionContent ~> toLByteString ~> addResLZ
382 366
                             next afterSection
383  
-                        else do next afterSection
384  
-                    Just (MuLambdaM func) -> 
385  
-                        if normalSection then do
  367
+                        Just (MuLambdaM func) -> do
386 368
                             res <- lift (func sectionContent)
387 369
                             toLByteString res ~> addResLZ
388 370
                             next afterSection
389  
-                        else do next afterSection
390  
-                    _ -> next afterSection
  371
+                        _ -> next afterSection
  372
+                    else case readContext of
  373
+                        Just (MuList []) -> do 
  374
+                            processBlock sectionContent
  375
+                                contexts otag ctag conf
  376
+                            next afterSection
  377
+                        Just (MuBool False) -> do 
  378
+                            processBlock sectionContent
  379
+                                contexts otag ctag conf
  380
+                            next afterSection
  381
+                        Nothing -> do
  382
+                            processBlock sectionContent
  383
+                                contexts otag ctag conf
  384
+                            next afterSection
  385
+                        _ -> next afterSection
391 386
     -- set delimiter
392 387
     | symb == ord8 '=' = 
393 388
         let
@@ -395,12 +390,11 @@ renderBlock contexts symb inTag afterClose otag ctag conf
395 390
             delimitersCommand = take (lenInTag - 1) inTag ~> drop 1
396 391
             getDelimiter = do
397 392
                 guard (lenInTag > 4)
398  
-                guard ((index inTag $ lenInTag - 1) == ord8 '=')
  393
+                guard (index inTag (lenInTag - 1) == ord8 '=')
399 394
                 [newOTag,newCTag] <- Just $ split (ord8 ' ') 
400 395
                     delimitersCommand
401 396
                 Just (newOTag, newCTag)
402  
-        in do
403  
-            case getDelimiter of
  397
+        in case getDelimiter of
404 398
                 Nothing -> next afterClose
405 399
                 Just (newOTag, newCTag) -> 
406 400
                     processBlock (trim' afterClose) contexts
@@ -411,7 +405,7 @@ renderBlock contexts symb inTag afterClose otag ctag conf
411 405
             fileName' = tail inTag ~> trimAll
412 406
             fileName'' = case muTemplateFileExt conf of
413 407
                 Nothing -> fileName'
414  
-                Just ext -> fileName' `append` (encodeStr ext)
  408
+                Just ext -> fileName' `append` encodeStr ext
415 409
             fileName = decodeStr fileName''
416 410
             fullFileName = case muTemplateFileDir conf of
417 411
                 Nothing -> fileName
@@ -426,12 +420,14 @@ renderBlock contexts symb inTag afterClose otag ctag conf
426 420
     | otherwise = do
427 421
         readVar contexts (trimAll inTag) ~> muEscapeFunc conf ~> addResLZ
428 422
         next afterClose
429  
-    where
  423
+  where
430 424
     next t = processBlock t contexts otag ctag conf
431 425
     trim' content = 
432 426
         dropWhile trimCharsTest content
433  
-        ~> \t -> if (length t > 0 && head t == ord8 '\n')
  427
+        ~> \t -> if length t > 0 && head t == ord8 '\n'
434 428
             then tail t else content
  429
+    processSection = undefined
  430
+            
435 431
 
436 432
 -- | Render Hastache template from ByteString
437 433
 hastacheStr :: (MonadIO m) => 
2  tests/test.hs
@@ -365,6 +365,7 @@ nestedGenericContextTest = do
365 365
         \Top variable : {{topDataTop}}\n\
366 366
         \Nested variable : {{nestedDataNested}}\n\
367 367
         \{{/topDataItems}}\n\
  368
+        \no context : {{^noCtx}}Should render{{/noCtx}}\n\
368 369
         \"
369 370
     context = mkGenericContext $ TopData {
370 371
         topDataTop = "TOP",
@@ -380,6 +381,7 @@ nestedGenericContextTest = do
380 381
         \-- Nested section\n\
381 382
         \Top variable : TOP\n\
382 383
         \Nested variable : NESTED_TWO\n\
  384
+        \no context : Should render\n\
383 385
         \"
384 386
 
385 387
 tests = TestList [

0 notes on commit 27ddb46

Please sign in to comment.
Something went wrong with that request. Please try again.