packages feed

subtitleParser 0.1.1 → 0.2

raw patch · 6 files changed

+169/−82 lines, 6 filesPVP ok

version bump matches the API change (PVP)

API changes (from Hackage documentation)

- Text.Subtitles.Datatypes: Line :: Int -> Range -> Text -> Line
- Text.Subtitles.Datatypes: Range :: Time -> Time -> Range
- Text.Subtitles.Datatypes: Time :: Int -> Int -> Int -> Int -> Time
- Text.Subtitles.Datatypes: data Line
- Text.Subtitles.Datatypes: data Range
- Text.Subtitles.Datatypes: data Time
- Text.Subtitles.Datatypes: frame :: Time -> Int
- Text.Subtitles.Datatypes: from :: Range -> Time
- Text.Subtitles.Datatypes: hour :: Time -> Int
- Text.Subtitles.Datatypes: index :: Line -> Int
- Text.Subtitles.Datatypes: instance Eq Line
- Text.Subtitles.Datatypes: instance Eq Range
- Text.Subtitles.Datatypes: instance Eq Time
- Text.Subtitles.Datatypes: instance Ord Line
- Text.Subtitles.Datatypes: instance Ord Range
- Text.Subtitles.Datatypes: instance Ord Time
- Text.Subtitles.Datatypes: instance Show Line
- Text.Subtitles.Datatypes: instance Show Range
- Text.Subtitles.Datatypes: instance Show Time
- Text.Subtitles.Datatypes: minutes :: Time -> Int
- Text.Subtitles.Datatypes: range :: Line -> Range
- Text.Subtitles.Datatypes: seconds :: Time -> Int
- Text.Subtitles.Datatypes: subs :: Line -> Text
- Text.Subtitles.Datatypes: to :: Range -> Time
- Text.Subtitles.Datatypes: type Subtitles = [Line]
- Text.Subtitles.SRT: parseSubtitles :: Parser Subtitles
+ Text.Subtitles.SRT: parseSRT :: Parser Subtitles
+ Text.Subtitles.SRT.Datatypes: Line :: Int -> Range -> Text -> Line
+ Text.Subtitles.SRT.Datatypes: Range :: Time -> Time -> Range
+ Text.Subtitles.SRT.Datatypes: Time :: Int -> Int -> Int -> Int -> Time
+ Text.Subtitles.SRT.Datatypes: data Line
+ Text.Subtitles.SRT.Datatypes: data Range
+ Text.Subtitles.SRT.Datatypes: data Time
+ Text.Subtitles.SRT.Datatypes: dialog :: Line -> Text
+ Text.Subtitles.SRT.Datatypes: frame :: Time -> Int
+ Text.Subtitles.SRT.Datatypes: from :: Range -> Time
+ Text.Subtitles.SRT.Datatypes: hour :: Time -> Int
+ Text.Subtitles.SRT.Datatypes: index :: Line -> Int
+ Text.Subtitles.SRT.Datatypes: instance Eq Line
+ Text.Subtitles.SRT.Datatypes: instance Eq Range
+ Text.Subtitles.SRT.Datatypes: instance Eq Time
+ Text.Subtitles.SRT.Datatypes: instance Ord Line
+ Text.Subtitles.SRT.Datatypes: instance Ord Range
+ Text.Subtitles.SRT.Datatypes: instance Ord Time
+ Text.Subtitles.SRT.Datatypes: instance Show Line
+ Text.Subtitles.SRT.Datatypes: instance Show Range
+ Text.Subtitles.SRT.Datatypes: instance Show Time
+ Text.Subtitles.SRT.Datatypes: minutes :: Time -> Int
+ Text.Subtitles.SRT.Datatypes: range :: Line -> Range
+ Text.Subtitles.SRT.Datatypes: seconds :: Time -> Int
+ Text.Subtitles.SRT.Datatypes: to :: Range -> Time
+ Text.Subtitles.SRT.Datatypes: type Subtitles = [Line]
+ Text.Subtitles.SUB: parseSUB :: Parser Subtitles
+ Text.Subtitles.SUB: parseSingleLine :: Parser Line
+ Text.Subtitles.SUB.Datatypes: Line :: Frame -> Frame -> Text -> Line
+ Text.Subtitles.SUB.Datatypes: data Line
+ Text.Subtitles.SUB.Datatypes: dialog :: Line -> Text
+ Text.Subtitles.SUB.Datatypes: finalFrame :: Line -> Frame
+ Text.Subtitles.SUB.Datatypes: instance Show Line
+ Text.Subtitles.SUB.Datatypes: startFrame :: Line -> Frame
+ Text.Subtitles.SUB.Datatypes: type Frame = Int
+ Text.Subtitles.SUB.Datatypes: type Subtitles = [Line]

Files

− Text/Subtitles/Datatypes.hs
@@ -1,70 +0,0 @@--- |--- Module      : Text.Subtitles.Datatypes--- Copyright   : Ruben Astudillo 2012--- License     : BSD3------ Maintainer  : ruben.astud@gmail.com--- Portability : unknown------ Common ADT for the project. Also serves as a place to  provide instance--- declarations for the ADTs.--module Text.Subtitles.Datatypes -  (-  -- * Re-exported datatypes.-  module Data.Attoparsec.Text,-  module Data.Text,--  -- * Datatypes-  Subtitles,-  Line(..),-  Range(..),-  Time(..)-  ) where--import Data.List (intercalate)-import Data.Text (Text, unpack)-import Data.Attoparsec.Text (Parser)--data Time = Time-  { hour    :: Int-  , minutes :: Int-  , seconds :: Int-  , frame   :: Int-  } deriving (Eq, Ord)--data Range = Range -  { from :: Time-  , to   :: Time-  } deriving (Eq, Ord)---- | The core of the parser. each one of the constructor representing one part--- of the Line-data Line = Line-  { index  :: Int-  , range  :: Range-  , subs   :: Text-  } deriving (Eq, Ord)--type Subtitles = [Line]--instance Show Time where-  show (Time h m s f) = concat [showT h, ":", showT m, ":", showT s, ",", showF f]--instance Show Range where-  show (Range f t) = concat [show f, " --> ", show t]--instance Show Line where-  show (Line i t s) = intercalate "\n" (show i : show t : [unpack s]) ++ "\n"--{- showT stands for showTime. In the parsing process "00" is read as "0", and- - that tailing 0 is lost unless we add it manually when showing -}-showT :: Int -> String-showT a | a < 10    = '0' : show a-        | otherwise = show a--{- showF stands for showFrame -}-showF :: Int -> String-showF a | a < 10    = '0':'0': show a-        | a < 100   = '0': show a-        | otherwise = show a
Text/Subtitles/SRT.hs view
@@ -14,10 +14,10 @@   -- $example      -- * Re-exported Datatypes-  module Text.Subtitles.Datatypes,+  module Text.Subtitles.SRT.Datatypes,    -- * Main parsers-  parseSubtitles,+  parseSRT,   parseSingleLine   ) where @@ -26,12 +26,12 @@ import Data.Attoparsec.Text  import qualified Data.Text as T -- in project modules-import Text.Subtitles.Datatypes+import Text.Subtitles.SRT.Datatypes  -- $example -- -- All the sections of a Line have their corresponding ADT in--- "Text.Subtitles.Datatypes"+-- "Text.Subtitles.SRT.Datatypes" --  -- >2 -- >00:00:50,050 --> 00:00:52,217@@ -50,14 +50,14 @@  -- |Main Parser, gives you a list of all the Lines of the subtitle. It fails if --  the subtitle doesn't have any Lines.-parseSubtitles :: Parser Subtitles-parseSubtitles = many1 parseSingleLine+parseSRT :: Parser Subtitles+parseSRT = many1 parseSingleLine  -- |The individual Line parser. given the upper example return the -- corresponding Line representation parseSingleLine :: Parser Line parseSingleLine = -  Line <$> parseIndex <*> parseRange <* eol <*> parseSubs T.empty+  Line <$> parseIndex <*> parseRange <* eol <*> parseDialog T.empty  parseIndex :: Parser Int parseIndex = decimal <* eol@@ -80,13 +80,13 @@  {- return the dialog checking for newlines that could be in there. that why is  - written in a monad instead of applicative. more efficient version welcome -}-parseSubs :: Text -> Parser Text-parseSubs t = do +parseDialog :: Text -> Parser Text+parseDialog t = do    line <- takeWhile1 (not . isEndOfLine)   endOfLine   let lineState = T.append t (T.snoc line '\n')   next <- anyChar   case next of     '\n' -> return lineState -    _    -> parseSubs (T.snoc lineState next)+    _    -> parseDialog (T.snoc lineState next) 
+ Text/Subtitles/SRT/Datatypes.hs view
@@ -0,0 +1,70 @@+-- |+-- Module      : Text.Subtitles.SRT.Datatypes+-- Copyright   : Ruben Astudillo 2012+-- License     : BSD3+--+-- Maintainer  : ruben.astud@gmail.com+-- Portability : unknown+--+-- ADT for .srt files. Also serves as a place to  provide instance+-- declarations for the ADTs.++module Text.Subtitles.SRT.Datatypes +  (+  -- * Re-exported datatypes.+  module Data.Attoparsec.Text,+  module Data.Text,++  -- * Datatypes+  Subtitles,+  Line(..),+  Range(..),+  Time(..)+  ) where++import Data.List (intercalate)+import Data.Text (Text, unpack)+import Data.Attoparsec.Text (Parser)++data Time = Time+  { hour    :: Int+  , minutes :: Int+  , seconds :: Int+  , frame   :: Int+  } deriving (Eq, Ord)++data Range = Range +  { from :: Time+  , to   :: Time+  } deriving (Eq, Ord)++-- | The core of the parser. each one of the constructor representing one part+-- of the Line+data Line = Line+  { index  :: Int+  , range  :: Range+  , dialog   :: Text+  } deriving (Eq, Ord)++type Subtitles = [Line]++instance Show Time where+  show (Time h m s f) = concat [showT h, ":", showT m, ":", showT s, ",", showF f]++instance Show Range where+  show (Range f t) = concat [show f, " --> ", show t]++instance Show Line where+  show (Line i t s) = intercalate "\n" (show i : show t : [unpack s]) ++ "\n"++{- showT stands for showTime. In the parsing process "00" is read as "0", and+ - that tailing 0 is lost unless we add it manually when showing -}+showT :: Int -> String+showT a | a < 10    = '0' : show a+        | otherwise = show a++{- showF stands for showFrame -}+showF :: Int -> String+showF a | a < 10    = '0':'0': show a+        | a < 100   = '0': show a+        | otherwise = show a
+ Text/Subtitles/SUB.hs view
@@ -0,0 +1,49 @@+-- |+-- Module      : Text.Subtitles.SUB+-- Copyright   : Ruben Astudillo 2012+-- License     : BSD3+--+-- Maintainer  : ruben.astud@gmail.com+-- Portability : unknown+--+-- A basic parser for .sub files (subtitles) based on 'Attoparsec' and 'Text'++module Text.Subtitles.SUB +  (+  -- * Terminology of the module+  -- $example ++  -- *Re-exported Datatypes+  module Text.Subtitles.SUB.Datatypes,+  +  -- * Main Parsers+  parseSingleLine,+  parseSUB +  ) where++import Control.Applicative+import Data.Attoparsec.Text++import Text.Subtitles.SUB.Datatypes++-- $example+--+-- Refering to the parts of a single line of a subtitle file.+--+-- >{50}{100}Drama here!+--+-- the first to numbers correspond to the frame in which the text Drama here!+-- is displayed. ++frame :: Parser Frame+frame = char '{' *> decimal <* char '}'++-- | Given the example return the corresponding Line representation. At the+-- moment this not handles modifiers as underlines or bold text+parseSingleLine :: Parser Line +parseSingleLine = Line <$> frame <*> frame <*> takeTill isEndOfLine++-- | Main parser of .sub files, given a .sub file it return a list of the dialog+-- lines+parseSUB :: Parser Subtitles+parseSUB = sepBy1 parseSingleLine endOfLine
+ Text/Subtitles/SUB/Datatypes.hs view
@@ -0,0 +1,36 @@+-- |+-- Module      : Text.Subtitles.SUB.Datatypes+-- Copyright   : Ruben Astudillo 2012+-- License     : BSD3+--+-- Maintainer  : ruben.astud@gmail.com+-- Portability : unknown+--+-- ADT for .sub files. Also serves as a place to  provide instance +-- declarations for the ADTs.++module Text.Subtitles.SUB.Datatypes +  (+  -- * Re-exported datatypes+  module Data.Attoparsec.Text,+  module Data.Text,+  +  -- * Datatypes+  Frame,+  Subtitles,+  Line(..)+  ) where++import Data.Text (Text)+import Data.Attoparsec.Text (Parser)++type Subtitles = [Line]++{- Should this be a newtype? or just a type? -}+type Frame = Int++data Line = Line { startFrame :: Frame,+                   finalFrame :: Frame,+                   dialog     :: Text }+  deriving (Show)+
subtitleParser.cabal view
@@ -1,5 +1,5 @@ name:            subtitleParser-version:         0.1.1+version:         0.2 license:         BSD3 license-file:    LICENSE category:        Text, Parsing@@ -29,7 +29,9 @@                  attoparsec    exposed-modules: Text.Subtitles.SRT-                   Text.Subtitles.Datatypes+                   Text.Subtitles.SRT.Datatypes+                   Text.Subtitles.SUB+                   Text.Subtitles.SUB.Datatypes    ghc-options: -Wall