NewFile initializes a File.
(src string, config *Config)
| 50 | |
| 51 | // NewFile initializes a File. |
| 52 | func NewFile(src string, config *Config) (*File, error) { |
| 53 | var format, ext string |
| 54 | var fbytes []byte |
| 55 | var lookup bool |
| 56 | path := src |
| 57 | |
| 58 | if system.FileExists(src) { |
| 59 | fbytes, _ = os.ReadFile(src) |
| 60 | if config.Flags.InExt != ".txt" { |
| 61 | ext, format = FormatFromExt(config.Flags.InExt, config.Formats) |
| 62 | } else { |
| 63 | ext, format = FormatFromExt(src, config.Formats) |
| 64 | } |
| 65 | } else { |
| 66 | fbytes = []byte(src) |
| 67 | lookup = true |
| 68 | // For stdin, allow an explicit path override to drive path-based config. |
| 69 | if config.Flags.InPath != "" { |
| 70 | path = config.Flags.InPath |
| 71 | } else { |
| 72 | path = "stdin" + config.Flags.InExt |
| 73 | } |
| 74 | // If --ext was explicitly set, respect it; otherwise infer from the path. |
| 75 | if config.Flags.InExt != ".txt" { |
| 76 | ext, format = FormatFromExt(config.Flags.InExt, config.Formats) |
| 77 | } else { |
| 78 | ext, format = FormatFromExt(path, config.Formats) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | filepaths := []string{path} |
| 83 | normed := system.ReplaceFileExt(path, config.Formats) |
| 84 | |
| 85 | baseStyles := config.GBaseStyles |
| 86 | checks := make(map[string]bool) |
| 87 | |
| 88 | for _, fp := range filepaths { |
| 89 | for _, sec := range config.StyleKeys { |
| 90 | if pat, found := config.SecToPat[sec]; found && pat.Match(fp) { |
| 91 | baseStyles = config.SBaseStyles[sec] |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | for _, sec := range config.RuleKeys { |
| 96 | if pat, found := config.SecToPat[sec]; found && pat.Match(fp) { |
| 97 | for k, v := range config.SChecks[sec] { |
| 98 | checks[k] = v |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | lang := "en" |
| 105 | for syntax, code := range config.FormatToLang { |
| 106 | sec, err := glob.Compile(syntax) |
| 107 | if err != nil { |
| 108 | return &File{}, err |
| 109 | } else if sec.Match(path) { |
searching dependent graphs…