Documentation
¶
Overview ¶
Package transforms mutates the in-flight asset tree between a source and a sink. Every transform reads and writes Context.FetchedPath.
Index ¶
- Constants
- func DisambiguatedSlug(name string) string
- func IsLicenceFile(name string) bool
- func LicenceFiles(root string) ([]string, error)
- func ResolveNameSlugs(records []CLDRRecord) map[string]string
- func SlugifyGroup(group string) string
- func SlugifyName(name string) string
- type CLDRRecord
- type Categorise
- type Exclude
- type ExcludeMetadata
- type FindLicences
- type FlattenVariants
- type Indexer
- type Sanitise
- type Slugify
- type SplitVariant
- type SplitVariants
- type SubsetTo
- type Variant
Constants ¶
const DefaultMaxStrayTextFiles = 25
DefaultMaxStrayTextFiles bounds how many files one Sanitise may rewrite to drop stray text. OpenMoji 15.1.0 had one such file in 4,000-odd; a release with dozens is malformed, and rewriting it quietly would hide that.
const LicenceSnapshotDir = "licence-files"
LicenceSnapshotDir is where FindLicences copies the licence files it found, inside the pipeline's working directory but outside the fetched tree.
const UncategorisedDir = "uncategorised"
UncategorisedDir is where Preserve keeps icons the taxonomy does not name.
Variables ¶
This section is empty.
Functions ¶
func DisambiguatedSlug ¶ added in v0.1.3
DisambiguatedSlug is SlugifyName with nothing dropped: every character the base slug would lose, other than a separator, is spelled out -- by name from symbolNames, else as u plus its hex code point. "keycap: *" is keycap-asterisk; "mark: \u2042" is mark-u2042.
func IsLicenceFile ¶ added in v0.1.2
IsLicenceFile reports whether a root-level filename names a licence or notice.
func LicenceFiles ¶ added in v0.1.2
LicenceFiles lists the licence and notice files directly under root, as absolute paths in a stable order. Subdirectories are not searched: a licence file deep in an upstream tree usually belongs to a dependency or a fixture, not to the icons.
func ResolveNameSlugs ¶ added in v0.1.3
func ResolveNameSlugs(records []CLDRRecord) map[string]string
ResolveNameSlugs gives every taxonomy record its file slug, keyed by CLDRRecord.Key, so that no two records share one.
SlugifyName keeps only [a-z0-9], so two names that differ only in a symbol slug alike: "keycap: #" and "keycap: *" are both "keycap", and renaming the second over the first lost an icon. On a collision the first record seen, in emoji-test.txt order, keeps the base slug -- the shipped packs carry "keycap: #" as keycap.svg -- and each later one takes a name read from its own: DisambiguatedSlug, so "keycap: *" is keycap-asterisk. Where that still collides, the record's codepoint key is appended, which is unique.
Categorise and Indexer both call this, so the files and the lookups name the same slug for every emoji.
func SlugifyGroup ¶
SlugifyGroup slugs a CLDR group name.
Python has two near-copies of this -- categorise's replaces "&" with a space and then spaces with hyphens, indexer's only does the first. They produce identical output for every CLDR group because SlugifyName already collapses any run of non-alphanumerics to a single hyphen, so the second replace is redundant. One function here, rather than two that are equal by luck.
func SlugifyName ¶
SlugifyName is the shared name-to-slug rule, exported because the CLDR categoriser and the indexer both need it and must agree.
Types ¶
type CLDRRecord ¶
CLDRRecord is one fully-qualified emoji from Unicode's emoji-test.txt.
func ParseCLDR ¶
func ParseCLDR(text string) []CLDRRecord
ParseCLDR reads emoji-test.txt into records, carrying the group heading forward across the rows beneath it.
func (CLDRRecord) Key ¶
func (r CLDRRecord) Key() string
Key is the codepoint sequence as it appears in an upstream filename.
type Categorise ¶
type Categorise struct {
pipeline.TransformKind
// By is "cldr" or "passthrough".
By string
// Preserve opts into never dropping an icon. Off by default, which is the
// behaviour twemoji-sets (Twemoji) relies on: exact match only, and an icon
// with no taxonomy entry is dropped and counted missing.
//
// On, two things change. A file whose name differs from a taxonomy entry
// only by the emoji-presentation selector (FE0F) is matched to it --
// OpenMoji ships 2600.svg where emoji-test.txt has 2600 FE0F, which left
// the sun, the wastebasket and ~200 more without a category. And an icon
// still unmatched, such as an OpenMoji extra or an emoji newer than the
// taxonomy, is kept in UncategorisedDir under its upstream name and counted
// as "uncategorised".
Preserve bool
// MaxUncategorised fails the run when more than this fraction of the icons
// ended up uncategorised, which is what a taxonomy of the wrong release
// looks like. Zero disables it. Only read with Preserve.
MaxUncategorised float64
// Force lets a run past MaxUncategorised. Nothing is dropped either way.
Force bool
Log *slog.Logger
}
Categorise regroups a flat emoji tree into Unicode CLDR category directories, renaming each file to the emoji's slug.
Upstream ships one flat directory named by codepoint; the packs file icons under a category so a picker can group them. Requires a UnicodeCldr source to have populated the taxonomy first.
func (Categorise) Name ¶
func (Categorise) Name() string
type Exclude ¶ added in v0.1.3
type Exclude struct {
pipeline.TransformKind
// Names are file stems (no .svg) dropped wherever they occur.
Names []string
// Dirs are directory names: a file with one of them anywhere in its
// path, relative to the fetched tree, is dropped.
Dirs []string
Metadata *ExcludeMetadata
}
Exclude drops icons from the tree by name, by directory or by upstream metadata.
It exists for the icons a pack must not ship although upstream does: trademarks (Octicons' GitHub marks), artwork under another licence (Simple Icons' non-CC0 logos), and directories whose licence is not stated (Game Icons' badges/). Every reason is in the pack config beside the rule, and every drop is counted, so a pack never shrinks without the run saying so.
type ExcludeMetadata ¶ added in v0.1.3
type ExcludeMetadata struct {
// File is the data file, relative to the upstream's root (before any
// source_path narrowed the tree). It must hold a JSON array of objects.
File string
// Name is the dot-path, inside each object, to the icon's file stem.
Name string
// Field is the dot-path to the value tested, and Allow the values kept.
// An object without Field is kept: the upstream's own licence covers it.
Field string
Allow []string
}
ExcludeMetadata names icons to drop from a data file the upstream ships, so the exclusion follows upstream instead of a list copied out of it once.
Simple Icons is the case: data/simple-icons.json records a licence for the handful of logos that are not CC0, and those must stay out of a CC0 pack. A hand-kept list of 212 slugs would be wrong the first release a logo is added or relicensed; the data file is right by construction.
type FindLicences ¶ added in v0.1.2
type FindLicences struct {
pipeline.TransformKind
// Dir is where the licence files sit, relative to the root, for an
// upstream whose icons carry a licence of their own in a sub-directory.
// Noto Emoji is the case: its root LICENSE is the OFL and covers the
// fonts, while 2D/svg/LICENSE is the Apache-2.0 notice governing the
// SVGs. Empty means the root.
Dir string
}
FindLicences records the upstream's own licence files, before anything narrows the tree.
Upstreams keep LICENSE, NOTICE and the like at the root of the package or archive, while the icons sit in a subdirectory that SubsetTo or FlattenVariants then makes the fetched path. Run after either and the licence is already out of reach, which is how every pack came to ship its icons without the text MIT, ISC, Apache-2.0, BSD and OFL require to travel with them. So this runs first, looks only at the root, and hands the paths forward for the LicenceTexts sink. It reads nothing and changes nothing.
It also records the untouched root as upstream_root, for the stages that read an upstream data file after the tree has been narrowed.
func (FindLicences) Execute ¶ added in v0.1.2
func (t FindLicences) Execute(ctx *pipeline.Context) error
func (FindLicences) Name ¶ added in v0.1.2
func (FindLicences) Name() string
type FlattenVariants ¶
type FlattenVariants struct {
pipeline.TransformKind
// Variants are the directories to merge. Order is irrelevant to the
// result and the output is written deterministically regardless.
Variants []Variant
// Separator normalises the character upstream puts between words, when
// the pack spells them differently. `_` and a space are rewritten to it.
//
// google-material-design-icons needs this and the other two do not:
// upstream ships `18_up_rating`, the pack committed `18-up-rating`, and
// without the rewrite only 607 of 2168 names in each variant line up.
Separator string
// Slug rewrites each stem with SlugifyName before the affix is added:
// lowercase, and every run of anything but a letter or digit becomes one
// hyphen. It replaces Separator when both are set.
//
// The order is the point. Health Icons ships `!.svg`, and the pack
// committed it as `-filled.svg`: the stem slugs to nothing and then takes
// the suffix. Slugging after the merge would trim that leading hyphen and
// write `filled.svg` instead, so a separate slugify step cannot reproduce
// the pack.
Slug bool
}
FlattenVariants merges several upstream directories into one flat tree, distinguishing them by an affix on each filename.
Some packs vendor a multi-directory upstream as a single flat set, and a manifest path cannot express that: SubsetTo picks one directory, so pointing it at heroicons' `24/solid` would refresh a quarter of the set and the sink would delete the other three quarters. Measured, those sets are not approximations of one variant but exact merges of all of them --
heroicons 1288 = 324 o- + 324 s- + 324 m- + 316 c-, a perfect round trip teeny-icons 1200 = 600 bare + 600 -o, likewise gmdi 10610 of 10751 committed, the remainder retired upstream
-- so the rule is recoverable exactly rather than being a guess about what the pack maintainer once did by hand.
func (FlattenVariants) Name ¶
func (FlattenVariants) Name() string
type Indexer ¶
type Indexer struct {
pipeline.TransformKind
// Targets is any of "codepoints" and "names".
Targets []string
// OutDirKey names an Extras key holding the output directory. When empty
// or absent, the files land beside the fetched tree.
OutDirKey string
}
Indexer writes the lookup files a picker needs beside the icons.
type Sanitise ¶
type Sanitise struct {
pipeline.TransformKind
// AlsoStripClass removes the class attribute as well. Off by default.
AlsoStripClass bool
// Strict turns policy violations into a failure. Off by default, because
// an upstream that ships one odd icon should not block the other 6,000 --
// the violation is reported either way.
Strict bool
// ReportOnly counts what the policy would remove without writing anything.
//
// For the aggregator pack, which has never been sanitised: its committed
// icons keep comments the policy strips, including fontawesome's licence
// attribution, so enforcing the policy during a refresh would rewrite tens
// of thousands of files and discard that attribution. Measured, all 50
// fontawesome icons sampled change under the policy. Reporting keeps the
// security picture visible without turning a refresh into a rewrite; the
// decision to enforce is then someone's to take deliberately.
ReportOnly bool
// MaxStrayTextFiles is the most files one run may rewrite to drop stray
// text (see svg.StrayText); zero means DefaultMaxStrayTextFiles. Force
// lets a run past it.
MaxStrayTextFiles int
Force bool
// DropUnparsable removes a file the sanitiser cannot parse instead of
// leaving it as upstream shipped it. Off by default. It is the stricter
// choice, not a looser one: a file that cannot be parsed cannot be
// checked, so with it on such a file never reaches the pack. Noto Emoji
// needs it, for 170 SVGs that declare Adobe Illustrator entities in an
// internal DTD. The count is in the run's metrics and logged per file.
DropUnparsable bool
Log *slog.Logger
}
Sanitise applies the SVG policy to every file in the tree, in place.
type Slugify ¶
type Slugify struct {
pipeline.TransformKind
// Dirs slugs directory names as well as file names.
Dirs bool
// HyphenateDots turns a dot inside the stem into a hyphen too.
HyphenateDots bool
}
Slugify lowercases and hyphenates SVG filenames in place.
The default rule keeps dots, so `lucene.net.svg` stays as it is. Two options widen it for upstreams whose names need more: HyphenateDots slugs the stem with SlugifyName, the rule the indexer uses (`lucene.net` becomes `lucene-net`), and Dirs slugs every directory name by the same rule, for an upstream that keeps its categories in folders such as `Game & Sports`.
Two names that slug to one fail the run, naming both. Renaming over an existing file would drop one of them at an unchanged count, which nothing later in the run would notice.
type SplitVariant ¶ added in v0.1.3
type SplitVariant struct {
// Path is the output directory the matching files are written to.
Path string
// From is the upstream directory the files are read from, relative to the
// fetched tree. Empty means the tree's root.
From string
// Suffix selects the files whose stem ends with it, and is stripped from
// the name written. An empty suffix takes every file in From that no
// other variant reading From claimed.
Suffix string
// FlattenPrefix reads From's sub-directories too, prefixing each file's
// name with its sub-directory path (watson-health/view.svg is written as
// watson-health-view.svg). Without it only From's own files are read.
// Every variant reading the same From must agree on it.
FlattenPrefix bool
}
SplitVariant is one output directory of SplitVariants, and the rule that decides which upstream files land in it.
type SplitVariants ¶ added in v0.1.3
type SplitVariants struct {
pipeline.TransformKind
Variants []SplitVariant
}
SplitVariants sorts an upstream's files into one directory per variant, by the directory they come from and the suffix their name carries.
It is FlattenVariants in the other direction. Upstreams spell a variant three ways -- as a directory (heroicons' 24/outline), as a name suffix in a shared directory (ionicons' bell-outline, Fluent's access_time_24_regular), or both at once (Phosphor's thin/bell-thin, Material Symbols' outlined/home-fill) -- and the packs keep every variant as a directory of bare names. One rule covers all of them: read From, claim each file for the variant with the longest matching suffix, strip it, write it under Path.
A file no variant claims is dropped, and that is how a pack takes a subset: Fluent ships ten sizes and the pack keeps 24 by naming only the _24_ suffixes. The drop is counted per source directory and logged, so a subset is visible in the run summary rather than silent.
A variant that claims nothing fails the run. That is what an upstream renaming its suffixes looks like, and carrying on would empty a directory of the pack at the next sync.
func (SplitVariants) Execute ¶ added in v0.1.3
func (t SplitVariants) Execute(ctx *pipeline.Context) error
func (SplitVariants) Name ¶ added in v0.1.3
func (SplitVariants) Name() string
type SubsetTo ¶
type SubsetTo struct {
pipeline.TransformKind
Subdir string
}
SubsetTo re-points FetchedPath at a subdirectory of the fetched tree.
This is how a source's `source_path` is honoured: npm ships @tabler/icons with the SVGs under icons/, and the rest of the tarball is not wanted.
type Variant ¶
type Variant struct {
// Subdir is the directory inside the fetched tree.
Subdir string
// Prefix and Suffix are applied to the file stem. A variant carrying
// neither is the unmarked one -- heroicons marks all four, while
// teenyicons leaves `solid` bare and suffixes only `outline`.
Prefix string
Suffix string
}
Variant is one upstream directory contributing to a flattened set, and the affix that distinguishes its icons from the other variants' once they share a directory.