<commit_before>{-# LANGUAGE OverloadedStrings #-}

module Handler.Publications where

import Import
import Text.Blaze.Html5 ((!))
import qualified Text.Blaze.Html5 as H (span, a)
import qualified Text.Blaze.Html5.Attributes as A (href, class_)
import Text.Parsec.Text ()
import Text.Parsec (parse)
import Text.BibTeX.Parse (splitAuthorList, skippingLeadingSpace, file)
import Text.BibTeX.Entry (T, entryType, identifier, fields)
import qualified Text.BibTeX.Format as F
import Data.Maybe (fromMaybe)
import Data.List (intersperse, intercalate)
import Text.Highlighting.Kate (highlightAs, formatHtmlBlock, defaultFormatOpts)
import Handler.Types (Composer)
import qualified Data.Text as T (unpack)

publicationsComposer :: Composer
publicationsComposer contentRaw = let res = parse (skippingLeadingSpace file) "publication handler" $ T.unpack contentRaw
                                      entries = case res of
                                                  Left _ -> error "failed to parse"
                                                  Right parsedEntries -> parsedEntries
                                  in mapM_ publicationWidget entries

publicationWidget :: T -> Widget
publicationWidget ent =
  let typ              = entryType ent
      ide              = filter (/= ':') $ identifier ent
      entLookup        = (`bibtexLookup` ent)
      auths            = splitAuthorList $ entLookup "author"
      title            = toHtml $ entLookup "title"
      authors          = toHtml $ intersperse ", " $ map formatAuthor auths
      venue            = formatVenue ent
      maybeRepository  = lookup "repository" $ fields ent
      maybeCopyright   = lookup "copyright" $ fields ent
      abstract         = entLookup "abstract"
      fieldsNoAbstract = filter (\(x, _) -> not $ x `elem` ["abstract", "repository", "copyright"]) $ fields ent
      bibtex           = toHtml $ formatHtmlBlock defaultFormatOpts
                        $ highlightAs "bibtex" $ F.entry ent{fields = fieldsNoAbstract}
  in $(widgetFile "publication")

knownAuthors :: [(String, Html -> Html)]
knownAuthors = [ ("<NAME>", H.span ! A.class_ "me")
               , ("<NAME>.", H.a ! A.href "http://www.prism.gatech.edu/~jshamma3/" ! A.class_ "external")<commit_msg>Formats the BibTeX entry nicely<commit_after>{-# LANGUAGE OverloadedStrings #-}

module Handler.Publications where

import Import
import Text.Blaze.Html5 ((!))
import qualified Text.Blaze.Html5 as H (span, a)
import qualified Text.Blaze.Html5.Attributes as A (href, class_)
import Text.Parsec.Text ()
import Text.Parsec (parse)
import Text.BibTeX.Parse (splitAuthorList, skippingLeadingSpace, file)
import Text.BibTeX.Entry (T(Cons), entryType, identifier, fields)
import Data.Maybe (fromMaybe)
import Data.List (intersperse, intercalate)
import Text.Highlighting.Kate (highlightAs, formatHtmlBlock, defaultFormatOpts)
import Handler.Types (Composer)
import qualified Data.Text as T (unpack)

publicationsComposer :: Composer
publicationsComposer contentRaw = let res = parse (skippingLeadingSpace file) "publication handler" $ T.unpack contentRaw
                                      entries = case res of
                                                  Left _ -> error "failed to parse"
                                                  Right parsedEntries -> parsedEntries
                                  in mapM_ publicationWidget entries

publicationWidget :: T -> Widget
publicationWidget ent =
  let typ              = entryType ent
      ide              = filter (/= ':') $ identifier ent
      entLookup        = (`bibtexLookup` ent)
      auths            = splitAuthorList $ entLookup "author"
      title            = toHtml $ entLookup "title"
      authors          = toHtml $ intersperse ", " $ map formatAuthor auths
      venue            = formatVenue ent
      maybeRepository  = lookup "repository" $ fields ent
      maybeCopyright   = lookup "copyright" $ fields ent
      abstract         = entLookup "abstract"
      fieldsNoAbstract = filter (\(x, _) -> not $ x `elem` ["abstract", "repository", "copyright"]) $ fields ent
      bibtex           = toHtml $ formatHtmlBlock defaultFormatOpts
                        $ highlightAs "bibtex" $ formatEntry ent{fields = fieldsNoAbstract}
  in $(widgetFile "publication")

formatEntry :: T -> String
formatEntry (Cons bibType bibId items) = "@" ++ bibType ++ "{" ++ bibId ++ ",\n" ++
                                         (concat $ intersperse ",\n" $ map formatItem items) ++ "\n}\n"

formatItem :: (String, String) -> String
formatItem (name, value) = "  "  ++  name ++ " = " ++ enclose value
    where enclose = if name `elem` ["year", "month"]
                    then id
                    else \val -> "\"" ++ val ++ "\""

knownAuthors :: [(String, Html -> Html)]
knownAuthors = [ ("<NAME>", H.span ! A.class_ "me")
               , ("<NAME>.", H.a ! A.href "http://www.prism.gatech.edu/~jshamma3/" ! A.class_ "external")
{'fraction_non_alphanumeric': 0.10634441087613293, 'fraction_numerical': 0.0016112789526686808, 'mean_word_length': 2.339609952925353, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1514378', 'n_tokens_mistral': 1350, 'n_tokens_neox': 1294, 'n_words': 396}