// Inserts license header to file represented by filename func insertLicense(filename string, replacer *strings.Replacer, config *Config) error { licensedFile := bytes.NewBuffer(nil) lcopyright, err := Asset(filepath.Join("licenses", string(config.LicenseType)+".copyright")) if err == nil { err = prependEOLComment(licensedFile, config, lcopyright) if err != nil { return err } } lheader, err := Asset(filepath.Join("licenses", string(config.LicenseType)+".header")) if err == nil { err := prependEOLComment(licensedFile, config, lheader) if err != nil { return err } } data, err := ioutil.ReadFile(filename) _, err = licensedFile.WriteString(string(data)) if err != nil { return err } filedata := replacer.Replace(licensedFile.String()) return ioutil.WriteFile(filename, []byte(filedata), 0640) }
// writeTo serializes the character data entity to the writer. func (c *CharData) writeTo(w *bufio.Writer, s *WriteSettings) { var r *strings.Replacer if s.CanonicalText { r = xmlReplacerCanonicalText } else { r = xmlReplacerNormal } w.WriteString(r.Replace(c.Data)) }
func (n *ninjaString) ValueWithEscaper(pkgNames map[*packageContext]string, escaper *strings.Replacer) string { str := escaper.Replace(n.strings[0]) for i, v := range n.variables { str += "${" + v.fullName(pkgNames) + "}" str += escaper.Replace(n.strings[i+1]) } return str }
// ParseMemoryMap parses a memory map in the format of // /proc/self/maps, and overrides the mappings in the current profile. // It renumbers the samples and locations in the profile correspondingly. func (p *Profile) ParseMemoryMap(rd io.Reader) error { b := bufio.NewReader(rd) var attrs []string var r *strings.Replacer const delimiter = "=" for { l, err := b.ReadString('\n') if err != nil { if err != io.EOF { return err } if l == "" { break } } if l = strings.TrimSpace(l); l == "" { continue } if r != nil { l = r.Replace(l) } m, err := parseMappingEntry(l) if err != nil { if err == errUnrecognized { // Recognize assignments of the form: attr=value, and replace // $attr with value on subsequent mappings. if attr := strings.SplitN(l, delimiter, 2); len(attr) == 2 { attrs = append(attrs, "$"+strings.TrimSpace(attr[0]), strings.TrimSpace(attr[1])) r = strings.NewReplacer(attrs...) } // Ignore any unrecognized entries continue } return err } if m == nil || (m.File == "" && len(p.Mapping) != 0) { // In some cases the first entry may include the address range // but not the name of the file. It should be followed by // another entry with the name. continue } if len(p.Mapping) == 1 && p.Mapping[0].File == "" { // Update the name if this is the entry following that empty one. p.Mapping[0].File = m.File continue } p.Mapping = append(p.Mapping, m) } p.remapLocationIDs() p.remapFunctionIDs() p.remapMappingIDs() return nil }
// writeTo serializes the attribute to the writer. func (a *Attr) writeTo(w *bufio.Writer, s *WriteSettings) { if a.Space != "" { w.WriteString(a.Space) w.WriteByte(':') } w.WriteString(a.Key) w.WriteString(`="`) var r *strings.Replacer if s.CanonicalAttrVal { r = xmlReplacerCanonicalAttrVal } else { r = xmlReplacerNormal } w.WriteString(r.Replace(a.Value)) w.WriteByte('"') }
// ParseProcMaps parses a memory map in the format of /proc/self/maps. // ParseMemoryMap should be called after setting on a profile to // associate locations to the corresponding mapping based on their // address. func ParseProcMaps(rd io.Reader) ([]*Mapping, error) { var mapping []*Mapping b := bufio.NewReader(rd) var attrs []string var r *strings.Replacer const delimiter = "=" for { l, err := b.ReadString('\n') if err != nil { if err != io.EOF { return nil, err } if l == "" { break } } if l = strings.TrimSpace(l); l == "" { continue } if r != nil { l = r.Replace(l) } m, err := parseMappingEntry(l) if err != nil { if err == errUnrecognized { // Recognize assignments of the form: attr=value, and replace // $attr with value on subsequent mappings. if attr := strings.SplitN(l, delimiter, 2); len(attr) == 2 { attrs = append(attrs, "$"+strings.TrimSpace(attr[0]), strings.TrimSpace(attr[1])) r = strings.NewReplacer(attrs...) } // Ignore any unrecognized entries continue } return nil, err } if m == nil { continue } mapping = append(mapping, m) } return mapping, nil }
// Inserts license header to file represented by filename func insertLicense(filename string, replacer *strings.Replacer, config *Config) error { licensedFile := bytes.NewBuffer(nil) lcopyright, err := Asset(filepath.Join("licenses", string(config.LicenseType)+".copyright")) cr := false if err == nil { err = prependEOLComment(licensedFile, config.EOLCommentStyle, []byte(replacer.Replace(string(lcopyright)))) if err != nil { return err } cr = true } lheader, err := Asset(filepath.Join("licenses", string(config.LicenseType)+".header")) if err == nil { plus := "" if cr { plus = "\n" } err := prependEOLComment(licensedFile, config.EOLCommentStyle, []byte(replacer.Replace(plus+string(lheader)))) if err != nil { return err } } // Extra newline for separating license code from package docs. licensedFile.WriteByte('\n') // Only use the replacer for the license, not the whole file. fh, err := os.Open(filename) if err != nil { return err } _, err = io.Copy(licensedFile, fh) fh.Close() if err != nil { return err } return ioutil.WriteFile(filename, licensedFile.Bytes(), 0640) }
func parseProcMapsFromScanner(s *bufio.Scanner) ([]*Mapping, error) { var mapping []*Mapping var attrs []string var r *strings.Replacer const delimiter = "=" for s.Scan() { line := strings.TrimSpace(s.Text()) if line == "" { continue } if r != nil { line = r.Replace(line) } m, err := parseMappingEntry(line) if err != nil { if err == errUnrecognized { // Recognize assignments of the form: attr=value, and replace // $attr with value on subsequent mappings. if attr := strings.SplitN(line, delimiter, 2); len(attr) == 2 { attrs = append(attrs, "$"+strings.TrimSpace(attr[0]), strings.TrimSpace(attr[1])) r = strings.NewReplacer(attrs...) } // Ignore any unrecognized entries continue } return nil, err } if m == nil { continue } mapping = append(mapping, m) } if err := s.Err(); err != nil { return nil, err } return mapping, nil }
func pageRenderLinks(content string) (res string) { re := regexp.MustCompile("\\[\\[.*\\]\\]") links := re.FindAllString(content, -1) for _, include := range links { title := strings.Trim(include, "[] ") renderAsLink := false if title[0] == '@' { renderAsLink = true title = title[1:] } isFile := false if len(title) > 5 && title[0:5] == "file:" { isFile = true title = title[5:] } var r *strings.Replacer if renderAsLink { if isFile { r = strings.NewReplacer(include, "/file/get/"+title) } else { r = strings.NewReplacer(include, "/page/"+title) } } else { if isFile { r = strings.NewReplacer(include, "<a href=\"/file/get/"+title+"\" >"+title+"</a>") } else { r = strings.NewReplacer(include, "<a href=\"/page/"+title+"\" >"+title+"</a>") } } content = r.Replace(content) } res = content return }
// SetPlaceholder fix @placeholder in post values func (p *Page) SetPlaceholder(htmlReplacer *strings.Replacer) { p.contentBytes = []byte(htmlReplacer.Replace(string(p.contentBytes))) }
// SetPlaceholder fix @placeholder in post values func (p *Post) SetPlaceholder(stringReplacer, htmlReplacer *strings.Replacer) { p.Thumb = stringReplacer.Replace(p.Thumb) p.contentBytes = []byte(htmlReplacer.Replace(string(p.contentBytes))) p.briefBytes = []byte(htmlReplacer.Replace(string(p.briefBytes))) }