ECL, default scale, and preferred alignment entries
This commit is contained in:
@@ -187,6 +187,8 @@ func (ctx *wikiContext) applyWikiTemplateFilter(value any, filter string, state
|
||||
return joinWikiTemplateValue(value, firstArg(args)), nil
|
||||
case "count":
|
||||
return countWikiTemplateValue(value), nil
|
||||
case "alignments":
|
||||
return formatWikiTemplateAlignments(value)
|
||||
case "list":
|
||||
return wikiTemplateHTML(renderWikiTemplateList(value, firstArg(args))), nil
|
||||
case "ordinal":
|
||||
@@ -365,6 +367,124 @@ func joinWikiTemplateValue(value any, sep string) string {
|
||||
}
|
||||
}
|
||||
|
||||
func formatWikiTemplateAlignments(value any) (string, error) {
|
||||
values, err := wikiTemplateAlignmentValues(value)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(values) == 0 || (len(values) == 1 && values[0] == 0) {
|
||||
return "Any", nil
|
||||
}
|
||||
labels := make([]string, 0, len(values))
|
||||
for _, value := range values {
|
||||
if value == 0 {
|
||||
continue
|
||||
}
|
||||
label, ok := wikiTemplateAlignmentLabel(value)
|
||||
if !ok {
|
||||
return "", fmt.Errorf("unknown alignment value 0x%02X", value)
|
||||
}
|
||||
labels = append(labels, label)
|
||||
}
|
||||
if len(labels) == 0 {
|
||||
return "Any", nil
|
||||
}
|
||||
return strings.Join(labels, ", "), nil
|
||||
}
|
||||
|
||||
func wikiTemplateAlignmentValues(value any) ([]int, error) {
|
||||
if isNullLike(value) {
|
||||
return []int{0}, nil
|
||||
}
|
||||
switch typed := value.(type) {
|
||||
case map[string]any:
|
||||
for key := range typed {
|
||||
if key != "list" {
|
||||
return nil, fmt.Errorf("alignment list object contains unsupported key %q", key)
|
||||
}
|
||||
}
|
||||
return wikiTemplateAlignmentValues(typed["list"])
|
||||
case string:
|
||||
trimmed := strings.TrimSpace(typed)
|
||||
if trimmed == "" || trimmed == "0" || strings.EqualFold(trimmed, "0x00") {
|
||||
return []int{0}, nil
|
||||
}
|
||||
if strings.HasPrefix(strings.ToLower(trimmed), "0x") {
|
||||
hexText := trimmed[2:]
|
||||
if len(hexText)%2 != 0 {
|
||||
return nil, fmt.Errorf("alignment hex list %q must contain two-character chunks", typed)
|
||||
}
|
||||
values := make([]int, 0, len(hexText)/2)
|
||||
for index := 0; index < len(hexText); index += 2 {
|
||||
parsed, err := strconv.ParseInt(hexText[index:index+2], 16, 0)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("alignment hex list %q contains invalid chunk %q", typed, hexText[index:index+2])
|
||||
}
|
||||
values = append(values, int(parsed))
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
if strings.Contains(trimmed, ",") {
|
||||
parts := strings.Split(trimmed, ",")
|
||||
values := make([]int, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
parsed, err := parseConfiguredAlignmentValue(part)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
values = append(values, parsed)
|
||||
}
|
||||
return values, nil
|
||||
}
|
||||
parsed, err := parseConfiguredAlignmentValue(trimmed)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []int{parsed}, nil
|
||||
case []any:
|
||||
values := make([]int, 0, len(typed))
|
||||
for _, item := range typed {
|
||||
parsed, err := parseConfiguredAlignmentValue(item)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
values = append(values, parsed)
|
||||
}
|
||||
return values, nil
|
||||
default:
|
||||
parsed, err := parseConfiguredAlignmentValue(value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []int{parsed}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func wikiTemplateAlignmentLabel(value int) (string, bool) {
|
||||
switch value {
|
||||
case 0x0A:
|
||||
return "Lawful Good", true
|
||||
case 0x09:
|
||||
return "Neutral Good", true
|
||||
case 0x0C:
|
||||
return "Chaotic Good", true
|
||||
case 0x03:
|
||||
return "Lawful Neutral", true
|
||||
case 0x01:
|
||||
return "True Neutral", true
|
||||
case 0x05:
|
||||
return "Chaotic Neutral", true
|
||||
case 0x12:
|
||||
return "Lawful Evil", true
|
||||
case 0x11:
|
||||
return "Neutral Evil", true
|
||||
case 0x14:
|
||||
return "Chaotic Evil", true
|
||||
default:
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
|
||||
func renderWikiTemplateList(value any, class string) string {
|
||||
items := wikiTemplateListItems(value)
|
||||
if len(items) == 0 {
|
||||
|
||||
Reference in New Issue
Block a user