Skip to content

Commit b168fb6

Browse files
committed
Use tables instead of generating prose
1 parent 9c71333 commit b168fb6

File tree

1 file changed

+24
-92
lines changed

1 file changed

+24
-92
lines changed

src/Distribution/Server/Pages/Reverse.hs

Lines changed: 24 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -39,57 +39,19 @@ reverseHtmlUtil ReverseFeature{reverseResource} = ReverseHtmlUtil{..}
3939
-> LatestOrOld -- ^ Whether the ReverseDisplay was rendered for recent (OnlyLatest) or older (OnlyOlder) versions of the package.
4040
-> ReversePageRender -- ^ Obtained from a ReverseDisplay-rendering function.
4141
-> [Html]
42-
reversePackageRender pkgid packageLink isRecent (ReversePageRender renders counts total) =
43-
let packageAnchor = anchor ! [href $ packageLink pkgid] << display pkgid
44-
hasVersion = packageVersion pkgid /= nullVersion
45-
pkgname = packageName pkgid
46-
statLinks = paragraph <<
47-
[ toHtml "Check out the "
48-
, anchor ! [href $ reverseVerboseUri reverseResource "" pkgname] << "exhaustive listings for all versions"
49-
, toHtml $ " of " ++ display pkgid ++ " and its "
50-
, anchor ! [href $ reverseFlatUri reverseResource "" pkgname] << "indirect dependencies", toHtml "." ]
51-
versionBox = if hasVersion && total /= allCounts
52-
then thediv ! [theclass "notification"] << [toHtml $ "These statistics only apply to this version of " ++ display pkgname ++ ". See also ", anchor ! [href $ reverseNameUri reverseResource "" pkgname] << [toHtml "packages which depend on ", emphasize << "any", toHtml " version"], toHtml $ " (all " ++ show total ++ " of them)."]
53-
else noHtml
54-
allCounts = uncurry (+) counts
55-
otherCount = case total - allCounts of
56-
diff | diff > 0 -> paragraph << [show diff ++ " packages depend on versions of " ++ display (packageName pkgid) ++ " other than " ++ if hasVersion then display (packageVersion pkgid) else "the latest" ++ "."]
57-
_ -> noHtml
58-
(pageText, nonPageText) = (if isRecent == OnlyLatest then id else uncurry $ flip (,)) (recentText, nonRecentText)
59-
otherLink = if isRecent == OnlyLatest then reverseOldUri reverseResource "" pkgid else reverseUri reverseResource "" pkgid
60-
in h2 << (display pkgid ++ ": " ++ num allCounts "reverse dependencies" "reverse dependency"):versionBox:case counts of
61-
(0, 0) ->
62-
[ paragraph << [toHtml "No packages depend on ",
63-
packageAnchor, toHtml "."]
42+
reversePackageRender pkgid packageLink isRecent (ReversePageRender renders (count, count') total) =
43+
let allCounts = count + count'
44+
firstTh = toHtml ("Depend on the " <> (if packageVersion pkgid == nullVersion then "latest" else "given") <> " version")
45+
in h2 << (display pkgid ++ ": " ++ display allCounts ++ " reverse dependencies"):
46+
[ if isRecent == OnlyLatest
47+
then toHtml "No version specified, so showing reverse dependencies for latest version."
48+
else toHtml ""
49+
, table ! [ theclass "fancy" ]
50+
<< [ tr << [ th << firstTh, th << toHtml "Depend on other versions", th << toHtml "Total" ]
51+
, tr << [ td << toHtml (display count), td << toHtml (display count'), td << toHtml (display total) ]]
52+
, reverseTable
6453
]
65-
(0, count) ->
66-
paragraph << [toHtml "No packages depend on ",
67-
if hasVersion then noHtml else toHtml "some version of ",
68-
packageAnchor,
69-
toHtml $ pageText 0 ++ " However, ",
70-
altVersions count nonPageText otherLink,
71-
toHtml "."] : [otherCount, statLinks]
72-
(count, count') ->
73-
[ (paragraph<<) $ [ mainVersions count pageText packageAnchor, toHtml " (listed below)." ]
74-
++ if count' > 0 then [ toHtml " Additionally, "
75-
, altVersions count' nonPageText otherLink
76-
, toHtml $ ". That's " ++ show (count+count') ++ " in total." ]
77-
else []
78-
] ++ (if isRecent == OnlyLatest then [] else [paragraph << oldText]) ++ [otherCount, statLinks, reverseTable]
7954
where
80-
mainVersions count textFunc pkgLink = toHtml
81-
[ toHtml $ num count "packages depend on " "package depends on "
82-
, pkgLink
83-
, toHtml $ textFunc count
84-
]
85-
altVersions count textFunc altLink = toHtml
86-
[ anchor ! [href altLink] << num count "packages" "package"
87-
, toHtml $ num' count " depend on " " depends on " ++ display pkgid ++ textFunc count
88-
]
89-
recentText count = ' ':num' count "in their latest versions" "in its latest version"
90-
nonRecentText count = ' ':num' count "only in older or deprecated versions" "only in an older or deprecated version"
91-
oldText = "The latest version of each package below, which doesn't depend on " ++ display pkgid ++ ", is linked from the first column. The version linked from the second column is the one which has a dependency on " ++ display pkgid ++", but it's no longer the preferred installation candidate. Note that packages which depend on versions of " ++ display pkgid ++ " not uploaded to Hackage are treated as not depending on it at all."
92-
9355
reverseTable = thediv << table << reverseTableRows
9456
reverseTableRows =
9557
tr ! [theclass "fancy"] << [ th << "Package name", th << "Version", th << "Reverse dependencies" ] :
@@ -103,34 +65,20 @@ reverseHtmlUtil ReverseFeature{reverseResource} = ReverseHtmlUtil{..}
10365
renderStatus (Just UnpreferredVersion) = [theclass "unpreferred"]
10466
renderStatus _ = []
10567

68+
renderCount ReverseCount{totalCount, directCount} =
69+
table ! [ theclass "fancy" ]
70+
<< [ tr << [ th << firstTh, th << secondTh, th << toHtml "Total" ]
71+
, tr << [ td << toHtml (display directCount), td << toHtml (display (totalCount - directCount)), td << toHtml (display totalCount) ]]
72+
where
73+
firstTh = toHtml "Direct reverse dependencies"
74+
secondTh = toHtml "Indirect reverse dependencies"
75+
10676
reverseFlatRender :: PackageName -> (PackageName -> String) -> ReverseCount -> [(PackageName, Int)] -> [Html]
107-
reverseFlatRender pkgname packageLink (ReverseCount{totalCount, directCount}) pairs =
108-
h2 << (display pkgname ++ ": " ++ num totalCount "total reverse dependencies" "reverse dependency"):case (directCount, totalCount) of
109-
(0, 0) -> [paragraph << [toHtml "No packages depend on ", toPackage pkgname]]
110-
_ ->
111-
[ paragraph << if totalCount == directCount
112-
then [ toHtml "All packages which use "
113-
, toPackage pkgname
114-
, toHtml " depend on it "
115-
, anchor ! [href $ reverseNameUri reverseResource "" pkgname] << "directly"
116-
, toHtml $ ". " ++ onlyPackage totalCount
117-
]
118-
else [ toPackage pkgname
119-
, toHtml " has "
120-
, anchor ! [href $ reverseNameUri reverseResource "" pkgname] << num directCount "packages" "package"
121-
, toHtml $ " which directly " ++ num' directCount "depend" "depends" ++ " on it, but there are more packages which depend on "
122-
, emphasize << "those"
123-
, toHtml $ " packages. If you flatten the tree of reverse dependencies, you'll find "
124-
++ show totalCount ++ " packages which use " ++ display pkgname ++ ", and "
125-
++ show (totalCount-directCount) ++ " which do so without depending directly on it. All of these packages are listed below."
126-
]
127-
, paragraph << [toHtml "See also the ", anchor ! [href $ reverseVerboseUri reverseResource "" pkgname] << "exhaustive listings for all versions", toHtml $ " of " ++ display pkgname ++ "."]
128-
, reverseTable
129-
]
77+
reverseFlatRender pkgname packageLink revCount pairs =
78+
h2 << (display pkgname ++ ": " ++ "total reverse dependencies"):renderCount revCount:[reverseTable]
13079
where
131-
toPackage pkg = anchor ! [href $ packageLink pkg] << display pkg
13280

133-
onlyPackage count = if count == 1 then "There's only one:" else "There are " ++ show count ++ ":"
81+
toPackage pkg = anchor ! [href $ packageLink pkg] << display pkg
13482

13583
reverseTable = thediv << table << reverseTableRows
13684
reverseTableRows =
@@ -143,19 +91,9 @@ reverseHtmlUtil ReverseFeature{reverseResource} = ReverseHtmlUtil{..}
14391

14492
-- /package/:package/reverse/verbose
14593
reverseVerboseRender :: PackageName -> [Version] -> (PackageId -> String) -> ReverseCount -> (Map.Map Version (Set PackageIdentifier)) -> [Html]
146-
reverseVerboseRender pkgname allVersions packageLink (ReverseCount {totalCount, directCount}) versions =
94+
reverseVerboseRender pkgname allVersions packageLink revCount versions =
14795
h2 << (display pkgname ++ ": reverse dependency statistics"):
148-
[ case directCount of
149-
0 -> paragraph << [ toHtml "No packages depend on ", thisPackage, toHtml "." ]
150-
_ -> toHtml
151-
[ paragraph << [ anchor ! [href $ reverseNameUri reverseResource "" pkgname] << num directCount "packages" "package"
152-
, toHtml $ num' directCount " depend" " depends"
153-
, toHtml " directly on ", thisPackage, toHtml "." ]
154-
, paragraph << [ toHtml $ num (totalCount - directCount) "packages depend" "package depends" ++ " indirectly on " ++ display pkgname ++ "." ]
155-
, paragraph << [ anchor ! [href $ reverseFlatUri reverseResource "" pkgname] << num totalCount "packages" "package"
156-
, toHtml $ num' totalCount " depend" " depends" ++ " on " ++ display pkgname ++ " in total."
157-
]
158-
]
96+
[ renderCount revCount
15997
, versionTable
16098
, if length allVersions > limitVersions
16199
-- Why the oldest? Such that the package can be cached indefinitely without having to get invalidated.
@@ -165,8 +103,6 @@ reverseHtmlUtil ReverseFeature{reverseResource} = ReverseHtmlUtil{..}
165103

166104
where
167105
limitVersions = 10
168-
toPackage pkgid = anchor ! [href $ packageLink pkgid] << display pkgid
169-
thisPackage = toPackage (PackageIdentifier pkgname $ nullVersion)
170106

171107
versionTable = thediv << (table ! [theclass "fancy"]) << versionTableRows
172108
versionTableRows =
@@ -193,10 +129,6 @@ reverseHtmlUtil ReverseFeature{reverseResource} = ReverseHtmlUtil{..}
193129
((ulist <<) . mkListOfLinks)
194130
]
195131

196-
num, num' :: Int -> String -> String -> String
197-
num n plural singular = show n ++ " " ++ num' n plural singular
198-
num' n plural singular = if n == 1 then singular else plural
199-
200132
-- /packages/reverse
201133
reversePackagesRender :: (PackageName -> String) -> Int -> [(PackageName, ReverseCount)] -> [Html]
202134
reversePackagesRender packageLink pkgCount namesWithCounts =

0 commit comments

Comments
 (0)