Repository navigation
Expand file tree
/
Copy paththeme.go
More file actions
66 lines (61 loc) · 2.25 KB
/
Copy paththeme.go
File metadata and controls
66 lines (61 loc) · 2.25 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package wpry
import (
"io"
)
// Theme represents parsed [WordPress theme headers].
//
// [WordPress theme headers]: https://developer.wordpress.org/themes/classic-themes/basics/main-stylesheet-style-css/
type Theme struct {
Name string `json:"name,omitempty"`
URI string `json:"uri,omitempty"`
Author string `json:"author,omitempty"`
AuthorURI string `json:"author_uri,omitempty"`
Description string `json:"description,omitempty"`
Version string `json:"version,omitempty"`
RequiresWP string `json:"requires_wp,omitempty"`
TestedUpTo string `json:"tested_up_to,omitempty"`
RequiresPHP string `json:"requires_php,omitempty"`
License string `json:"license,omitempty"`
LicenseURI string `json:"license_uri,omitempty"`
TextDomain string `json:"text_domain,omitempty"`
DomainPath string `json:"domain_path,omitempty"`
Tags string `json:"tags,omitempty"`
Template string `json:"template,omitempty"`
}
// ParseTheme reads from r and attempts to extract WordPress theme headers. If a
// theme name is found it returns a populated [Theme] struct. Otherwise, an
// error is returned.
//
// The function mirrors WordPress [wp_get_theme] function:
// - CR is normalized to LF
// - best-effort encoding conversion is applied
// - only the first 8 KiB is read
//
// [wp_get_theme]: https://developer.wordpress.org/reference/functions/wp_get_theme/
func ParseTheme(r io.Reader) (Theme, error) {
s, err := read(r)
if err != nil {
return Theme{}, err
}
name := extractHeader(s, "theme_name")
if name == "" {
return Theme{}, errNoHeader
}
return Theme{
Name: name,
URI: extractHeader(s, "theme_uri"),
Description: extractHeader(s, "description"),
Version: extractHeader(s, "version"),
RequiresWP: extractHeader(s, "requires_at_least"),
RequiresPHP: extractHeader(s, "requires_php"),
Author: extractHeader(s, "author"),
AuthorURI: extractHeader(s, "author_uri"),
License: extractHeader(s, "license"),
LicenseURI: extractHeader(s, "license_uri"),
TextDomain: extractHeader(s, "text_domain"),
DomainPath: extractHeader(s, "domain_path"),
TestedUpTo: extractHeader(s, "tested_up_to"),
Tags: extractHeader(s, "tags"),
Template: extractHeader(s, "template"),
}, nil
}