Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#64] Copy paste detection in lists #102

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ default-extensions:
- ConstraintKinds
- DataKinds
- DefaultSignatures
- DeriveAnyClass
- DeriveDataTypeable
- DeriveGeneric
- DerivingStrategies
Expand Down
42 changes: 27 additions & 15 deletions src/Xrefcheck/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import Control.Lens (makeLenses)
import Data.Aeson (FromJSON (..), withText)
import Data.Char (isAlphaNum)
import Data.Char qualified as C
import Data.DList (DList)
import Data.DList qualified as DList
import Data.Default (Default (..))
import Data.List qualified as L
import Data.Map qualified as M
Expand All @@ -26,8 +28,6 @@ import Text.Numeral.Roman (toRoman)

import Xrefcheck.Progress
import Xrefcheck.Util
import Data.DList (DList)
import Data.DList qualified as DList

-----------------------------------------------------------
-- Types
Expand Down Expand Up @@ -62,6 +62,7 @@ instance FromJSON Flavor where
-- representation of this thing, and it actually appears in reports only.
newtype Position = Position (Maybe Text)
deriving stock (Show, Eq, Generic)
deriving anyclass NFData

instance Buildable Position where
build (Position pos) = case pos of
Expand All @@ -77,7 +78,9 @@ data Reference = Reference
, rAnchor :: Maybe Text
-- ^ Section or custom anchor tag.
, rPos :: Position
} deriving stock (Show, Generic)
}
deriving stock (Show, Generic)
deriving anyclass NFData

-- | Context of anchor.
data AnchorType
Expand All @@ -88,35 +91,50 @@ data AnchorType
| BiblioAnchor
-- ^ Id of entry in bibliography
deriving stock (Show, Eq, Generic)
deriving anyclass NFData

-- | A referable anchor.
data Anchor = Anchor
{ aType :: AnchorType
, aName :: Text
, aPos :: Position
} deriving stock (Show, Eq, Generic)
}
deriving stock (Show, Eq, Generic)
deriving anyclass NFData

data CopyPaste = CopyPaste
{ cpAnchorText :: Text
Copy link
Contributor

@alyoanton9 alyoanton9 Nov 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like AnchorText and PlainText sound confusing. The issue doesn't focus on anchors, but all type of links, external and internal. Perhaps, more generic LinkText and URLText would be better?

UPD: Okay, I'm not sure if all the links were intended to check, as there is only example with files in issue description. But it's also not clear that this feature is for files only

, cpPlainText :: Text
, cpPosition :: Position
}
deriving stock (Show, Eq, Generic)
deriving anyclass NFData

data FileInfoDiff = FileInfoDiff
{ _fidReferences :: DList Reference
, _fidAnchors :: DList Anchor
, _fidCopyPastes :: DList CopyPaste
}
makeLenses ''FileInfoDiff

diffToFileInfo :: FileInfoDiff -> FileInfo
diffToFileInfo (FileInfoDiff refs anchors) =
FileInfo (DList.toList refs) (DList.toList anchors)
diffToFileInfo (FileInfoDiff refs anchors pastas) =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: I believe here should be pastes

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🍝

FileInfo (DList.toList refs) (DList.toList anchors) (DList.toList pastas)

instance Semigroup FileInfoDiff where
FileInfoDiff a b <> FileInfoDiff c d = FileInfoDiff (a <> c) (b <> d)
FileInfoDiff a b e <> FileInfoDiff c d f = FileInfoDiff (a <> c) (b <> d) (e <> f)

instance Monoid FileInfoDiff where
mempty = FileInfoDiff mempty mempty
mempty = FileInfoDiff mempty mempty mempty

-- | All information regarding a single file we care about.
data FileInfo = FileInfo
{ _fiReferences :: [Reference]
, _fiAnchors :: [Anchor]
} deriving stock (Show, Generic)
, _fiCopyPastes :: [CopyPaste]
}
deriving stock (Show, Generic)
deriving anyclass NFData
makeLenses ''FileInfo

instance Default FileInfo where
Expand All @@ -129,12 +147,6 @@ newtype RepoInfo = RepoInfo (Map FilePath FileInfo)
-- Instances
-----------------------------------------------------------

instance NFData Position
instance NFData Reference
instance NFData AnchorType
instance NFData Anchor
instance NFData FileInfo

instance Buildable Reference where
build Reference{..} =
nameF ("reference " +| paren (build loc) |+ " " +| rPos |+ "") $
Expand Down
16 changes: 9 additions & 7 deletions src/Xrefcheck/Scanners/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,19 @@ nodeExtractInfo input@(Node _ _ nSubs) = do
let aType = HeaderAnchor lvl
let aName = headerToAnchor flavor $ nodeExtractText node
let aPos = toPosition pos
return $ FileInfoDiff DList.empty $ DList.singleton $ Anchor {aType, aName, aPos}
return mempty
{ _fidAnchors = DList.singleton $ Anchor {aType, aName, aPos}
}

HTML_INLINE text -> do
let mName = T.stripSuffix "\">" =<< T.stripPrefix "<a name=\"" text
case mName of
Just aName -> do
let aType = HandAnchor
aPos = toPosition pos
return $ FileInfoDiff
mempty
(pure $ Anchor {aType, aName, aPos})
return mempty
{ _fidAnchors = DList.singleton $ Anchor {aType, aName, aPos}
}

Nothing -> do
return mempty
Expand All @@ -167,9 +169,9 @@ nodeExtractInfo input@(Node _ _ nSubs) = do
[t] -> (t, Nothing)
t : ts -> (t, Just $ T.intercalate "#" ts)
[] -> error "impossible"
return $ FileInfoDiff
(DList.singleton $ Reference {rName, rPos, rLink, rAnchor})
DList.empty
return mempty
{ _fidReferences = DList.singleton $ Reference {rName, rPos, rLink, rAnchor}
}

_ -> return mempty

Expand Down