<commit_before>module Main exposing (..)

import Html exposing ( Html )
import Html.App as Html
import Http
import Task exposing (..)
import Debug
import String exposing (..)

-- import Html.Attributes exposing (..)
-- import Html.Events exposing (onClick)

import HvCss
import Html.CssHelpers


{ id, class, classList } = Html.CssHelpers.withNamespace "haskell-view"

main : Program Never
main =
    Html.program
        { init = init
        , view = view
        , update = update
        , subscriptions = subscriptions
        }

-- MODEL

type alias Location =
  { line : Int
  , col : Int
  }

type alias Token =
  { name : String
  , from : Location
  , to : Location
  }

type alias Model =
    { src : String
    , html : Html Msg
    , tokens : String
    }


initialModel : Model
initialModel =
  { src = ""
  , html = Html.text ""
  , tokens = ""
  }


init : ( Model, Cmd Msg )
init = ( initialModel, getHaskell "Lib" )


-- UPDATE

type Msg
  = GetHaskell
  | FetchSucceed String
  | FetchFail Http.Error

update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of

        GetHaskell ->
          (model, getHaskell "TODO")

        FetchSucceed data ->
           case (split "<EOF>" data) of
            [] -> (Model "err" (Html.text "err") "", Cmd.none)
            src :: tokens ->
              let t = case (List.head tokens) of
                        Just ts -> ts
                        Nothing -> "no tokens"
                  html = createView src t
                  in ((Model model.src (Debug.log "HTML\n" html) model.tokens), Cmd.none)

        FetchFail _ ->
          (model, Cmd.none)

-- HTTP

getHaskell : String -> Cmd Msg
getHaskell path =
  let url = "http://localhost:8081/source/" ++ path
  in
    Task.perform FetchFail FetchSucceed <| Http.getString url



-- SUBSCRIPTIONS

subscriptions : Model -> Sub Msg
subscriptions model =
    Sub.none



-- VIEW

view : Model -> Html Msg
view { src, html, tokens } = html

createView : String -> String -> Html Msg
createView src tokens =
    Html.div [] [
      Html.div [] (List.map viewLine (split "\n" src)),
      Html.div [] [Html.text ("TOKENS: " ++ tokens)]
    ]

viewLine : String -> Html Msg
viewLine line =
    Html.div [ class [HvCss.ITlineComment]] [ Html.text line, Html.br [] []]

<commit_msg>refactorings, simplify model<commit_after>module Main exposing (..)

import Html exposing (Html)
import Html.App as Html
import Http
import Task exposing (..)
import Debug
import String exposing (..)


-- import Html.Attributes exposing (..)
-- import Html.Events exposing (onClick)

import HvCss
import Html.CssHelpers


{ id, class, classList } =
    Html.CssHelpers.withNamespace "haskell-view"
main : Program Never
main =
    Html.program
        { init = init
        , view = view
        , update = update
        , subscriptions = subscriptions
        }



-- MODEL


type alias Model =
    { html : Html Msg }


type alias Location =
    { line : Int
    , col : Int
    }


type alias Token =
    { name : String
    , from : Location
    , to : Location
    }


initialModel : Model
initialModel =
    { html = Html.text "initializing.." }


init : ( Model, Cmd Msg )
init =
    ( initialModel, getHaskell "Lib" )



-- UPDATE


type Msg
    = GetHaskell
    | GetHaskellSucceed String
    | FetchFail Http.Error


update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
    case msg of
        GetHaskell ->
            ( model, getHaskell "TODO" )

        GetHaskellSucceed data ->
            case (split "<EOF>" data) of
                [] ->
                    ( Model (Html.text "error"), Cmd.none )

                src :: rest ->
                    let
                        tokens =
                            case (List.head rest) of
                                Just ts ->
                                    ts

                                Nothing ->
                                    "no tokens"

                        html =
                            createView src tokens
                    in
                        ( (Model (Debug.log "HTML\n" html)), Cmd.none )

        FetchFail _ ->
            ( model, Cmd.none )



-- perform HTTP


getHaskell : String -> Cmd Msg
getHaskell path =
    let
        url =
            "http://localhost:8081/source/" ++ path
    in
        Task.perform FetchFail GetHaskellSucceed <| Http.getString url



-- SUBSCRIPTIONS


subscriptions : Model -> Sub Msg
subscriptions model =
    Sub.none



-- VIEW


view : Model -> Html Msg
view { html } =
    html



-- create Html from update


createView : String -> String -> Html Msg
createView src tokens =
    Html.div []
        [ Html.div [] (List.map viewLine (split "\n" src))
        , Html.div [] [ Html.text ("TOKENS: " ++ tokens) ]
        ]


viewLine : String -> Html Msg
viewLine line =
    Html.div [ class [ HvCss.ITlineComment ] ] [ Html.text line, Html.br [] [] ]

{'fraction_non_alphanumeric': 0.10958071703463641, 'fraction_numerical': 0.0016204172574437918, 'mean_word_length': 1.707236842105263, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2062452', 'n_tokens_mistral': 1604, 'n_tokens_neox': 1494, 'n_words': 443}