FormatFromExt takes a file extension and returns its [normExt, format] list, if supported.
(path string, mapping map[string]string)
| 97 | // FormatFromExt takes a file extension and returns its [normExt, format] |
| 98 | // list, if supported. |
| 99 | func FormatFromExt(path string, mapping map[string]string) (string, string) { |
| 100 | base := strings.Trim(filepath.Ext(path), ".") |
| 101 | kind := getFormat("." + base) |
| 102 | |
| 103 | if format, found := mapping[base]; found { |
| 104 | if kind == "code" && getFormat("."+format) == "markup" { |
| 105 | // NOTE: This is a special case of embedded markup within code. |
| 106 | return "." + format, "fragment" |
| 107 | } else if kind == "data" && getFormat("."+format) == "markup" { |
| 108 | // NOTE: This is a special case of embedded markup within data. |
| 109 | // |
| 110 | // Unlike code, data formats are *always* linted as a fragment with |
| 111 | // the default format as plain text. |
| 112 | return "." + format, "data" |
| 113 | } |
| 114 | base = format |
| 115 | } |
| 116 | |
| 117 | base = "." + base |
| 118 | for r, f := range FormatByExtension { |
| 119 | m, _ := regexp.MatchString(r, base) |
| 120 | if m { |
| 121 | return f[0], f[1] |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | return "unknown", "unknown" |
| 126 | } |
| 127 | |
| 128 | func getFormat(ext string) string { |
| 129 | for r, f := range FormatByExtension { |
searching dependent graphs…