lektor_ng.types.special

src/lektor_ng/types/special.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from lektor_ng.types.primitives import SingleInputType
from lektor_ng.utils import Url, slugify


class SortKeyType(SingleInputType):
    widget = "integer"

    def value_from_raw(self, raw):
        if raw.value is None:
            return raw.missing_value("Missing sort key")
        try:
            return int(raw.value.strip())
        except ValueError:
            return raw.bad_value("Bad sort key value")


class SlugType(SingleInputType):
    widget = "slug"

    def value_from_raw(self, raw):
        if raw.value is None:
            return raw.missing_value("Missing slug")
        return slugify(raw.value)


class UrlType(SingleInputType):
    widget = "url"

    def value_from_raw(self, raw):
        if raw.value is None:
            return raw.missing_value("Missing URL")
        return Url.from_string(raw.value)