parseCSLCursor :: Cursor -> Style
parseCSLCursor cur =
  Style{ styleVersion = T.pack version
       , styleClass = T.pack class_
       , styleInfo = Just info
       , styleDefaultLocale = defaultLocale
       , styleLocale = locales
       , styleAbbrevs = Abbreviations M.empty
       , csOptions = filter (\(k,_) -> k `notElem`
                                       ["class",
                                        "xmlns",
                                        "version",
                                        "default-locale"]) $ parseOptions cur
       , csMacros = macros
       , citation = fromMaybe (Citation [] [] Layout{ layFormat = emptyFormatting
                                                    , layDelim = ""
                                                    , elements = [] }) $ listToMaybe $
                    cur $/ get "citation" &| parseCitation
       , biblio = listToMaybe $ cur $/ get "bibliography" &| parseBiblio
       }
  where version = unpack . T.concat $ cur $| laxAttribute "version"
        class_ = unpack . T.concat $ cur $| laxAttribute "class"
        defaultLocale = case cur $| laxAttribute "default-locale" of
                             (x:_) -> x
                             []    -> "en-US"
        author = case cur $// get "info" &/ get "author" of
                      (x:_) -> CSAuthor (T.concat $ x $/ get "name" &/ content)
                                 (T.concat $ x $/ get "email" &/ content)
                                 (T.concat $ x $/ get "uri"   &/ content)
                      _     -> CSAuthor "" "" ""
        info = CSInfo
          { csiTitle      = T.concat $ (cur $/ get "info" &/ get "title" &/ content)
          , csiAuthor     = author
          , csiCategories = []  -- TODO we don't really use this, and the type
                                -- in Style doesn't match current CSL at all
          , csiId         = T.concat $ cur $/ get "info" &/ get "id" &/ content
          , csiUpdated    = T.concat $ cur $/ get "info" &/ get "updated" &/ content
          }
        locales = cur $/ get "locale" &| parseLocaleElement
        macros  = cur $/ get "macro" &| parseMacroMap
parseCSLCursor :: Cursor -> Style
parseCSLCursor cur =
  Style{ styleVersion = T.pack version
       , styleClass = T.pack class_
       , styleInfo = Just info
       , styleDefaultLocale = defaultLocale
       , styleLocale = locales
       , styleAbbrevs = Abbreviations M.empty
       , csOptions = filter (\(k,_) -> k `notElem`
                                       ["class",
                                        "xmlns",
                                        "version",
                                        "default-locale"]) $ parseOptions cur
       , csMacros = macros
       , citation = fromMaybe (Citation [] [] Layout{ layFormat = emptyFormatting
                                                    , layDelim = ""
                                                    , elements = [] }) $ listToMaybe $
                    cur $/ get "citation" &| parseCitation
       , biblio = listToMaybe $ cur $/ get "bibliography" &| parseBiblio
       }
  where version = unpack . T.concat $ cur $| laxAttribute "version"
        class_ = unpack . T.concat $ cur $| laxAttribute "class"
        defaultLocale = case cur $| laxAttribute "default-locale" of
                             (x:_) -> x
                             []    -> "en-US"
        author = case cur $// get "info" &/ get "author" of
                      (x:_) -> CSAuthor (T.concat $ x $/ get "name" &/ content)
                                 (T.concat $ x $/ get "email" &/ content)
                                 (T.concat $ x $/ get "uri"   &/ content)
                      _     -> CSAuthor "" "" ""
        info = CSInfo
          { csiTitle      = T.concat $ (cur $/ get "info" &/ get "title" &/ content)
          , csiAuthor     = author
          , csiCategories = []  -- TODO we don't really use this, and the type
                                -- in Style doesn't match current CSL at all
          , csiId         = T.concat $ cur $/ get "info" &/ get "id" &/ content
          , csiUpdated    = T.concat $ cur $/ get "info" &/ get "updated" &/ content
          }
        locales = cur $/ get "locale" &| parseLocaleElement
        macros  = cur $/ get "macro" &| parseMacroMap
parseCSLCursor cur =
  Style{ styleVersion = T.pack version
       , styleClass = T.pack class_
       , styleInfo = Just info
       , styleDefaultLocale = defaultLocale
       , styleLocale = locales
       , styleAbbrevs = Abbreviations M.empty
       , csOptions = filter (\(k,_) -> k `notElem`
                                       ["class",
                                        "xmlns",
                                        "version",
                                        "default-locale"]) $ parseOptions cur
       , csMacros = macros
       , citation = fromMaybe (Citation [] [] Layout{ layFormat = emptyFormatting
                                                    , layDelim = ""
                                                    , elements = [] }) $ listToMaybe $
                    cur $/ get "citation" &| parseCitation
       , biblio = listToMaybe $ cur $/ get "bibliography" &| parseBiblio
       }
  where version = unpack . T.concat $ cur $| laxAttribute "version"
        class_ = unpack . T.concat $ cur $| laxAttribute "class"
        defaultLocale = case cur $| laxAttribute "default-locale" of
                             (x:_) -> x
                             []    -> "en-US"
        author = case cur $// get "info" &/ get "author" of
                      (x:_) -> CSAuthor (T.concat $ x $/ get "name" &/ content)
                                 (T.concat $ x $/ get "email" &/ content)
                                 (T.concat $ x $/ get "uri"   &/ content)
                      _     -> CSAuthor "" "" ""
        info = CSInfo
          { csiTitle      = T.concat $ (cur $/ get "info" &/ get "title" &/ content)
          , csiAuthor     = author
          , csiCategories = []  -- TODO we don't really use this, and the type
                                -- in Style doesn't match current CSL at all
          , csiId         = T.concat $ cur $/ get "info" &/ get "id" &/ content
          , csiUpdated    = T.concat $ cur $/ get "info" &/ get "updated" &/ content
          }
        locales = cur $/ get "locale" &| parseLocaleElement
        macros  = cur $/ get "macro" &| parseMacroMap