<commit_before>{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}

module Render.RichText
  ( Inlines (..),
    -- LinkTarget (..),
    space,
    text,
    -- link,
    linkRange,
    linkHole,
    icon,
    -- combinators
    (<+>),
    punctuate,
    braces,
    braces',
    dbraces,
    parens,
    mparens,
    indent,
    hcat,
    hsep,
    sep,
    fsep,
    vcat,
    fcat,
    -- symbols
    arrow,
    lambda,
    forallQ,
    showIndex,
    leftIdiomBrkt,
    rightIdiomBrkt,
    emptyIdiomBrkt,
  )
where

import qualified Agda.Interaction.Options.IORefs as Agda
import qualified Agda.Syntax.Position as Agda
import qualified Agda.Utils.FileName as Agda
import qualified Agda.Utils.Null as Agda
import Agda.Utils.Suffix (subscriptAllowed, toSubscriptDigit)
import Data.Aeson (ToJSON (toJSON), Value (Null))
import Data.Foldable (toList)
import Data.IORef (readIORef)
import Data.List (intersperse)
import Data.Sequence (Seq (..))
import qualified Data.Sequence as Seq
import qualified Data.Strict.Maybe as Strict
import Data.String (IsString (..))
import GHC.Generics (Generic)
import qualified GHC.IO.Unsafe as UNSAFE

--------------------------------------------------------------------------------

newtype RichText = RichText (Seq Block)

--------------------------------------------------------------------------------

newtype Block = Paragraph (Seq Inline)

--------------------------------------------------------------------------------

newtype Inlines = Inlines {unInlines :: Seq Inline}

-- Represent Inlines with String literals
instance IsString Inlines where
  fromString s = Inlines (Seq.singleton (Text s mempty))

instance Semigroup Inlines where
  Inlines xs <> Inlines ys = Inlines (merge xs ys)
    where
      merge :: Seq Inline -> Seq Inline -> Seq Inline
      merge Empty             ys                
        = ys
      merge (xs :|> Text s c) (Text t d :<| ys) 
        | c == d    = merge xs (Text (s <> t) c :<| ys)
        | otherwise = merge xs (Text s c :<| Text t d :<| ys) 
      merge (xs :|> x)        ys                
        = merge xs (x :<| ys)


  -- = Icon String ClassNames
  -- | Text String ClassNames
  -- | Link Agda.Range Inlines ClassNames
  -- | Hole Int
  -- | -- | Horizontal grouping, wrap when there's no space
  --   Horz [Inlines]
  -- | -- | Vertical grouping, each children would end with a newline
  --   Vert [Inlines]<commit_msg>[ change ] Increase merging in RichText<commit_after>{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}

module Render.RichText
  ( Inlines (..),
    -- LinkTarget (..),
    space,
    text,
    -- link,
    linkRange,
    linkHole,
    icon,
    -- combinators
    (<+>),
    punctuate,
    braces,
    braces',
    dbraces,
    parens,
    mparens,
    indent,
    hcat,
    hsep,
    sep,
    fsep,
    vcat,
    fcat,
    -- symbols
    arrow,
    lambda,
    forallQ,
    showIndex,
    leftIdiomBrkt,
    rightIdiomBrkt,
    emptyIdiomBrkt,
  )
where

import qualified Agda.Interaction.Options.IORefs as Agda
import qualified Agda.Syntax.Position as Agda
import qualified Agda.Utils.FileName as Agda
import qualified Agda.Utils.Null as Agda
import Agda.Utils.Suffix (subscriptAllowed, toSubscriptDigit)
import Data.Aeson (ToJSON (toJSON), Value (Null))
import Data.Foldable (toList)
import Data.IORef (readIORef)
import Data.List (intersperse)
import Data.Sequence (Seq (..))
import qualified Data.Sequence as Seq
import qualified Data.Strict.Maybe as Strict
import Data.String (IsString (..))
import GHC.Generics (Generic)
import qualified GHC.IO.Unsafe as UNSAFE

--------------------------------------------------------------------------------

newtype RichText = RichText (Seq Block)

--------------------------------------------------------------------------------

newtype Block = Paragraph (Seq Inline)

--------------------------------------------------------------------------------

newtype Inlines = Inlines {unInlines :: Seq Inline}

-- Represent Inlines with String literals
instance IsString Inlines where
  fromString s = Inlines (Seq.singleton (Text s mempty))

instance Semigroup Inlines where
  Inlines xs <> Inlines ys = Inlines (merge xs ys)
    where
      merge :: Seq Inline -> Seq Inline -> Seq Inline
      merge Empty      ys = ys
      merge (xs :|> x) ys = merge xs (cons x ys)

      cons :: Inline -> Seq Inline -> Seq Inline
      cons (Text s c) (Text t d :<| xs)
        -- merge 2 adjacent Text if they have the same classnames
        | c == d    = Text (s <> t) c :<| xs 
        | otherwise = Text s c :<| Text t d :<| xs
      cons (Text s c) (Horz [] :<| xs) = cons (Text s c) xs
      cons (Text s c) (Horz (Inlines t:ts) :<| xs)
        -- merge Text with Horz when possible
        = Horz (Inlines (cons (Text s c) t) :ts) :<| xs
      cons x xs = x :<| xs

{'fraction_non_alphanumeric': 0.1956035009159373, 'fraction_numerical': 0.0002035416242621616, 'mean_word_length': 1.8520023215322112, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3357553', 'n_tokens_mistral': 1571, 'n_tokens_neox': 1513, 'n_words': 477}