| 1 |
// Copyright 2018 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package metadecoders |
| 15 |
|
| 16 |
import ( |
| 17 |
"path/filepath" |
| 18 |
"strings" |
| 19 |
) |
| 20 |
|
| 21 |
type Format string |
| 22 |
|
| 23 |
const ( |
| 24 |
// These are the supported metadata formats in Hugo. Most of these are also |
| 25 |
// supported as /data formats. |
| 26 |
ORG Format = "org" |
| 27 |
JSON Format = "json" |
| 28 |
TOML Format = "toml" |
| 29 |
YAML Format = "yaml" |
| 30 |
CSV Format = "csv" |
| 31 |
XML Format = "xml" |
| 32 |
) |
| 33 |
|
| 34 |
// FormatFromStrings returns the first non-empty Format from the given strings. |
| 35 |
func FormatFromStrings(ss ...string) Format { |
| 36 |
for _, s := range ss { |
| 37 |
if f := FormatFromString(s); f != "" { |
| 38 |
return f |
| 39 |
} |
| 40 |
} |
| 41 |
return "" |
| 42 |
} |
| 43 |
|
| 44 |
// FormatFromString turns formatStr, typically a file extension without any ".", |
| 45 |
// into a Format. It returns an empty string for unknown formats. |
| 46 |
func FormatFromString(formatStr string) Format { |
| 47 |
formatStr = strings.ToLower(formatStr) |
| 48 |
if strings.Contains(formatStr, ".") { |
| 49 |
// Assume a filename |
| 50 |
formatStr = strings.TrimPrefix(filepath.Ext(formatStr), ".") |
| 51 |
} |
| 52 |
switch formatStr { |
| 53 |
case "yaml", "yml": |
| 54 |
return YAML |
| 55 |
case "json": |
| 56 |
return JSON |
| 57 |
case "toml": |
| 58 |
return TOML |
| 59 |
case "org": |
| 60 |
return ORG |
| 61 |
case "csv": |
| 62 |
return CSV |
| 63 |
case "xml": |
| 64 |
return XML |
| 65 |
} |
| 66 |
|
| 67 |
return "" |
| 68 |
} |
| 69 |
|
| 70 |
// FormatFromContentString tries to detect the format (JSON, YAML, TOML or XML) |
| 71 |
// in the given string. |
| 72 |
// It return an empty string if no format could be detected. |
| 73 |
func (d Decoder) FormatFromContentString(data string) Format { |
| 74 |
csvIdx := strings.IndexRune(data, d.Delimiter) |
| 75 |
jsonIdx := strings.Index(data, "{") |
| 76 |
yamlIdx := strings.Index(data, ":") |
| 77 |
xmlIdx := strings.Index(data, "<") |
| 78 |
tomlIdx := strings.Index(data, "=") |
| 79 |
|
| 80 |
if isLowerIndexThan(csvIdx, jsonIdx, yamlIdx, xmlIdx, tomlIdx) { |
| 81 |
return CSV |
| 82 |
} |
| 83 |
|
| 84 |
if isLowerIndexThan(jsonIdx, yamlIdx, xmlIdx, tomlIdx) { |
| 85 |
return JSON |
| 86 |
} |
| 87 |
|
| 88 |
if isLowerIndexThan(yamlIdx, xmlIdx, tomlIdx) { |
| 89 |
return YAML |
| 90 |
} |
| 91 |
|
| 92 |
if isLowerIndexThan(xmlIdx, tomlIdx) { |
| 93 |
return XML |
| 94 |
} |
| 95 |
|
| 96 |
if tomlIdx != -1 { |
| 97 |
return TOML |
| 98 |
} |
| 99 |
|
| 100 |
return "" |
| 101 |
} |
| 102 |
|
| 103 |
func isLowerIndexThan(first int, others ...int) bool { |
| 104 |
if first == -1 { |
| 105 |
return false |
| 106 |
} |
| 107 |
for _, other := range others { |
| 108 |
if other != -1 && other < first { |
| 109 |
return false |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
return true |
| 114 |
} |