예제 #1
1
파일: markdown.go 프로젝트: Julianzz/gogs
func RenderSpecialLink(rawBytes []byte, urlPrefix string) []byte {
	ms := MentionPattern.FindAll(rawBytes, -1)
	for _, m := range ms {
		rawBytes = bytes.Replace(rawBytes, m,
			[]byte(fmt.Sprintf(`<a href="/user/%s">%s</a>`, m[1:], m)), -1)
	}
	ms = commitPattern.FindAll(rawBytes, -1)
	for _, m := range ms {
		m = bytes.TrimSpace(m)
		i := strings.Index(string(m), "commit/")
		j := strings.Index(string(m), "#")
		if j == -1 {
			j = len(m)
		}
		rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
			` <code><a href="%s">%s</a></code>`, m, ShortSha(string(m[i+7:j])))), -1)
	}
	ms = issueFullPattern.FindAll(rawBytes, -1)
	for _, m := range ms {
		m = bytes.TrimSpace(m)
		i := strings.Index(string(m), "issues/")
		j := strings.Index(string(m), "#")
		if j == -1 {
			j = len(m)
		}
		rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
			` <a href="%s">#%s</a>`, m, ShortSha(string(m[i+7:j])))), -1)
	}
	ms = issueIndexPattern.FindAll(rawBytes, -1)
	for _, m := range ms {
		rawBytes = bytes.Replace(rawBytes, m, []byte(fmt.Sprintf(
			`<a href="%s/issues/%s">%s</a>`, urlPrefix, m[1:], m)), -1)
	}
	return rawBytes
}
예제 #2
0
파일: engine.go 프로젝트: Carbyn/iris
// Render a JSON response.
func (j JSON) Render(ctx *fasthttp.RequestCtx, v interface{}) error {
	if j.StreamingJSON {
		return j.renderStreamingJSON(ctx, v)
	}

	var result []byte
	var err error

	if j.Indent {
		result, err = json.MarshalIndent(v, "", "  ")
		result = append(result, '\n')
	} else {
		result, err = json.Marshal(v)
	}
	if err != nil {
		return err
	}

	// Unescape HTML if needed.
	if j.UnEscapeHTML {
		result = bytes.Replace(result, []byte("\\u003c"), []byte("<"), -1)
		result = bytes.Replace(result, []byte("\\u003e"), []byte(">"), -1)
		result = bytes.Replace(result, []byte("\\u0026"), []byte("&"), -1)
	}
	w := ctx.Response.BodyWriter()
	// JSON marshaled fine, write out the result.
	j.Head.Write(ctx)
	if len(j.Prefix) > 0 {
		w.Write(j.Prefix)
	}
	w.Write(result)
	return nil
}
예제 #3
0
func (fieldContext *FieldContextDef) doTerrainMap(websocketConnectionContext *websocketConnectionContextDef) *gkerr.GkErrDef {

	var gkErr *gkerr.GkErrDef

	var singleSession *ses.SingleSessionDef
	singleSession = fieldContext.sessionContext.GetSessionFromId(websocketConnectionContext.sessionId)

	var messageToClient *message.MessageToClientDef = new(message.MessageToClientDef)
	messageToClient.Command = message.SetTerrainMapReq
	var jsonFileName string = fieldContext.terrainSvgDir + string(os.PathSeparator) + "map_terrain_" + strconv.FormatInt(int64(singleSession.GetCurrentPodId()), 10) + ".json"
	messageToClient.JsonData, gkErr = gkcommon.GetFileContents(jsonFileName)
	if gkErr != nil {
		return gkErr
	}
	var lf []byte = []byte("\n")
	var tb []byte = []byte("\t")
	var sp []byte = []byte(" ")
	var nl []byte = []byte("")
	var te []byte = []byte("errain")
	var bj []byte = []byte("bject")
	//	Not typos

	messageToClient.JsonData = bytes.Replace(messageToClient.JsonData, lf, nl, -1)
	messageToClient.JsonData = bytes.Replace(messageToClient.JsonData, sp, nl, -1)
	messageToClient.JsonData = bytes.Replace(messageToClient.JsonData, tb, nl, -1)
	messageToClient.JsonData = bytes.Replace(messageToClient.JsonData, te, nl, -1)
	messageToClient.JsonData = bytes.Replace(messageToClient.JsonData, bj, nl, -1)
	fieldContext.queueMessageToClient(websocketConnectionContext.sessionId, messageToClient)

	return nil
}
예제 #4
0
파일: example_test.go 프로젝트: achanda/go
func ExampleReplace() {
	fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("k"), []byte("ky"), 2))
	fmt.Printf("%s\n", bytes.Replace([]byte("oink oink oink"), []byte("oink"), []byte("moo"), -1))
	// Output:
	// oinky oinky oink
	// moo moo moo
}
예제 #5
0
func CountPinterestPins(r *Request) *Result {
	var (
		resp    *http.Response
		err     error
		feed    pinterestFeed
		body    []byte
		feedUrl = fmt.Sprintf(pinterestFeedUrl, r.Url())
	)

	if resp, err = http.Get(feedUrl); err != nil {
		return Empty
	}
	if body, err = ioutil.ReadAll(resp.Body); err != nil {
		return Empty
	}

	body = bytes.Replace(body, []byte("receiveCount("), nil, 1)
	body = bytes.Replace(body, []byte(")"), nil, 1)

	if err = json.Unmarshal([]byte(body), &feed); err != nil {
		return Empty
	}

	return &Result{Points: feed.Count}
}
예제 #6
0
func writeTemplate() {
	jsonWords := ""
	nWords := len(foundWords)
	wid := 0
	for k, v := range foundWords {
		if wid%100 == 0 {
			fmt.Printf("compiling: %d / %d\n", wid, nWords)
		}
		wid++

		jsonWords += fmt.Sprintf("\"%s\": [\n", k)
		for _, w := range v {
			jsonWords += fmt.Sprintf("\t[ %d, %d, %d ],\n", w.start, w.step, w.length)
		}
		jsonWords += "],\n"
	}

	template, err := gas.ReadFile("github.com/thingalon/code-finder/template.html")
	if err != nil {
		fmt.Println(err)
		os.Exit(0)
	}

	template = bytes.Replace(template, []byte("[filename]"), []byte(os.Args[1]), 1)
	template = bytes.Replace(template, []byte("[content]"), content, 1)
	template = bytes.Replace(template, []byte("[words]"), []byte(jsonWords), 1)

	outfile := os.Args[1] + ".html"

	ioutil.WriteFile(outfile, template, 0644)

	fmt.Println("Written to " + outfile)
}
예제 #7
0
파일: engine.go 프로젝트: unrolled/render
// Render a JSON response.
func (j JSON) Render(w io.Writer, v interface{}) error {
	if j.StreamingJSON {
		return j.renderStreamingJSON(w, v)
	}

	var result []byte
	var err error

	if j.Indent {
		result, err = json.MarshalIndent(v, "", "  ")
		result = append(result, '\n')
	} else {
		result, err = json.Marshal(v)
	}
	if err != nil {
		return err
	}

	// Unescape HTML if needed.
	if j.UnEscapeHTML {
		result = bytes.Replace(result, []byte("\\u003c"), []byte("<"), -1)
		result = bytes.Replace(result, []byte("\\u003e"), []byte(">"), -1)
		result = bytes.Replace(result, []byte("\\u0026"), []byte("&"), -1)
	}

	// JSON marshaled fine, write out the result.
	if hw, ok := w.(http.ResponseWriter); ok {
		j.Head.Write(hw)
	}
	if len(j.Prefix) > 0 {
		w.Write(j.Prefix)
	}
	w.Write(result)
	return nil
}
예제 #8
0
파일: markdown.go 프로젝트: client9/gospell
func cleanupLine(s []byte) []byte {

	// strip away various headings from back and front
	s = leadingHeadline.ReplaceAll(s, nil)
	s = trailingHeadline.ReplaceAll(s, nil)

	// strip away leading "> > > " from block quotes
	s = blockQuote.ReplaceAll(s, nil)

	// is all "-", "=", "*", "|" make empty
	// this eliminates various HR variations and
	// table decoration and is not a word anyways
	if allSymbols.Match(s) {
		return []byte{}
	}

	s = simpleCode.ReplaceAll(s, nil)

	// there is no reason to NOT replace `*` `~` or `_` with a space character
	// not used in words
	s = bytes.Replace(s, []byte{'*'}, nil, -1)
	s = bytes.Replace(s, []byte{'~'}, nil, -1)
	s = bytes.Replace(s, []byte{'_'}, nil, -1)

	// links. 	[link](/MyURI)
	// Stuff inside the "link" can be on different lines, but "](/URI)"
	// is all on one line so we can delete ](....space )
	// ![ is for images
	s = bytes.Replace(s, []byte{'!', '['}, nil, -1)
	s = bytes.Replace(s, []byte{'['}, nil, -1)
	s = linkTarget.ReplaceAll(s, nil)
	return s
}
예제 #9
0
파일: main.go 프로젝트: hbdlb/qml-1
func processGlGo(dirName string) {
	data, err := ioutil.ReadFile(dirName + "/gl.go")
	if err != nil {
		log.Fatal("ioutil.ReadFile: ", err)
	}
	data, _ = format.Source(data)

	oldStr := "// #cgo pkg-config: Qt5Core Qt5OpenGL\n"
	newStr := "// #cgo !windows pkg-config: Qt5Core Qt5OpenGL\n// #cgo windows LDFLAGS: -L./goqgl -lgoqgl_{{.LibSuffix}}\n"

	newStr = strings.Replace(newStr, "{{.LibSuffix}}", libSuffix(dirName), -1)

	if *flagRevert {
		data = bytes.Replace(data, []byte(newStr), []byte(oldStr), -1)
		data, _ = format.Source(data)

		err = ioutil.WriteFile(dirName+"/gl.go", data, 0666)
		if err != nil {
			log.Fatal("ioutil.WriteFile: ", err)
		}
		return
	} else {
		data = bytes.Replace(data, []byte(oldStr), []byte(newStr), -1)
		data, _ = format.Source(data)

		err = ioutil.WriteFile(dirName+"/gl.go", data, 0666)
		if err != nil {
			log.Fatal("ioutil.WriteFile: ", err)
		}
		return
	}
}
예제 #10
0
파일: main.go 프로젝트: hbdlb/qml-1
func processGlGo_importPath(dirName string) {
	data, err := ioutil.ReadFile(dirName + "/gl.go")
	if err != nil {
		log.Fatal("ioutil.ReadFile: ", err)
	}
	data, _ = format.Source(data)

	oldStr := `"gopkg.in/qml.v1/gl/glbase"`
	newStr := `"github.com/chai2010/qml/gl/glbase"`

	if *flagRevert {
		data = bytes.Replace(data, []byte(newStr), []byte(oldStr), -1)
		data, _ = format.Source(data)

		err = ioutil.WriteFile(dirName+"/gl.go", data, 0666)
		if err != nil {
			log.Fatal("ioutil.WriteFile: ", err)
		}
		return
	} else {
		data = bytes.Replace(data, []byte(oldStr), []byte(newStr), -1)
		data, _ = format.Source(data)

		err = ioutil.WriteFile(dirName+"/gl.go", data, 0666)
		if err != nil {
			log.Fatal("ioutil.WriteFile: ", err)
		}
		return
	}
}
예제 #11
0
파일: targets.go 프로젝트: deyvsh/vegeta
// Request creates an *http.Request out of Target and returns it along with an
// error in case of failure.
func (t *Target) Request() (*http.Request, error) {
	// Dave's hack: replace tokens in body with unix times
	miliTimeClipStarted := time.Now().UnixNano()/1000000 - 60000
	t.Body = bytes.Replace(t.Body, []byte("__miliTimeClipStarted__"), []byte(strconv.Itoa(int(miliTimeClipStarted))), 1)
	nanoTimeNow := time.Now().UnixNano()
	miliTimeClipEnded := nanoTimeNow / 1000000
	t.Body = bytes.Replace(t.Body, []byte("__miliTimeClipEnded__"), []byte(strconv.Itoa(int(miliTimeClipEnded))), 2)
	microTimeNowString := strconv.Itoa(int(nanoTimeNow / 1000))
	splitPoint := len(microTimeNowString) - 6
	microTimeForStill := fmt.Sprint(microTimeNowString[:splitPoint], ".", microTimeNowString[splitPoint:])
	t.Body = bytes.Replace(t.Body, []byte("__microTimeForStill__"), []byte(microTimeForStill), 1)

	req, err := http.NewRequest(t.Method, t.URL, bytes.NewBuffer(t.Body))
	if err != nil {
		return nil, err
	}
	for k, vs := range t.Header {
		req.Header[k] = make([]string, len(vs))
		copy(req.Header[k], vs)
	}
	if host := req.Header.Get("Host"); host != "" {
		req.Host = host
	}
	return req, nil
}
예제 #12
0
파일: og.go 프로젝트: jmptrader/og
func (o *Og) CmdHelp() ([]byte, int) {
	if len(o.Args) > 0 {
		// TODO: add help shims for gen, parse
		return o.Default("help")
	}
	modifyHelp := func(help []byte, helps []string) []byte {
		search := regexp.MustCompile(`(?m)^.+?commands.+$`)
		idx := search.FindIndex(help)
		left, right := help[:idx[0]], help[idx[1]:]
		template := "%sOg commands:\n\n    %s\n\nGo commands:%s"
		tmp := fmt.Sprintf(template, left, strings.Join(helps, "\n    "), right)
		return []byte(tmp)
	}
	out, _ := exec.Command("go").CombinedOutput()
	out = bytes.Replace(out, []byte("Go"), []byte("Og"), 1)
	out = bytes.Replace(out, []byte("go command"), []byte("og command"), 1)
	out = bytes.Replace(out, []byte("go help"), []byte("og help"), -1)
	helps := []string{
		"gen         generate preprocessed source tree",
		"parse       preprocess one source file",
		"update      update og command",
	}
	out = modifyHelp(out, helps)
	return out, 0
}
예제 #13
0
func (this *Logger) Write(bs []byte) (int, error) {
	this.lock.Lock()
	defer this.lock.Unlock()

	prefix := this.prefix()
	suffix := this.suffix()

	final := bs
	if !this.written || this.lastEndedWithNewline {
		this.written = true
		final = append([]byte(prefix), final...)
	}
	if bytes.HasSuffix(final, []byte{byte('\n')}) {
		final = final[:len(final)-1]
		this.lastEndedWithNewline = true
	} else {
		this.lastEndedWithNewline = false
	}
	final = bytes.Replace(final, []byte("\r\n"), []byte("\n"), -1)
	final = bytes.Replace(final, []byte("\n"), []byte(suffix+"\n"+prefix), -1)
	if this.lastEndedWithNewline {
		final = append(final, []byte(suffix)...)
		final = append(final, '\n')
	}
	n, err := this.writer.Write(final)
	if n > len(bs) {
		n = len(bs)
	}
	return n, err
}
예제 #14
0
// GET /containers/{name:.*}/json
func getContainerJSON(c *context, w http.ResponseWriter, r *http.Request) {
	name := mux.Vars(r)["name"]
	container := c.cluster.Container(name)
	if container == nil {
		httpError(w, fmt.Sprintf("No such container %s", name), http.StatusNotFound)
		return
	}
	client, scheme := newClientAndScheme(c.tlsConfig)

	resp, err := client.Get(scheme + "://" + container.Node.Addr() + "/containers/" + container.Id + "/json")
	if err != nil {
		httpError(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// cleanup
	defer resp.Body.Close()
	defer closeIdleConnections(client)

	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		httpError(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// insert Node field
	data = bytes.Replace(data, []byte("\"Name\":\"/"), []byte(fmt.Sprintf("\"Node\":%s,\"Name\":\"/", cluster.SerializeNode(container.Node))), -1)

	// insert node IP
	data = bytes.Replace(data, []byte("\"HostIp\":\"0.0.0.0\""), []byte(fmt.Sprintf("\"HostIp\":%q", container.Node.IP())), -1)

	w.Header().Set("Content-Type", "application/json")
	w.Write(data)
}
예제 #15
0
파일: process.go 프로젝트: bigtiger/caddy
// defaultTemplate constructs a default template.
func defaultTemplate(c Config, metadata Metadata, requestPath string) []byte {
	var scripts, styles bytes.Buffer
	for _, style := range c.Styles {
		styles.WriteString(strings.Replace(cssTemplate, "{{url}}", style, 1))
		styles.WriteString("\r\n")
	}
	for _, script := range c.Scripts {
		scripts.WriteString(strings.Replace(jsTemplate, "{{url}}", script, 1))
		scripts.WriteString("\r\n")
	}

	// Title is first line (length-limited), otherwise filename
	title := metadata.Title
	if title == "" {
		title = filepath.Base(requestPath)
		if body, _ := metadata.Variables["markdown"].([]byte); len(body) > 128 {
			title = string(body[:128])
		} else if len(body) > 0 {
			title = string(body)
		}
	}

	html := []byte(htmlTemplate)
	html = bytes.Replace(html, []byte("{{title}}"), []byte(title), 1)
	html = bytes.Replace(html, []byte("{{css}}"), styles.Bytes(), 1)
	html = bytes.Replace(html, []byte("{{js}}"), scripts.Bytes(), 1)

	return html
}
예제 #16
0
func TestChaingMultipleTransformers(t *testing.T) {
	f1 := func(ct contentTransformer) {
		ct.Write(bytes.Replace(ct.Content(), []byte("f1"), []byte("f1r"), -1))
	}
	f2 := func(ct contentTransformer) {
		ct.Write(bytes.Replace(ct.Content(), []byte("f2"), []byte("f2r"), -1))
	}
	f3 := func(ct contentTransformer) {
		ct.Write(bytes.Replace(ct.Content(), []byte("f3"), []byte("f3r"), -1))
	}

	f4 := func(ct contentTransformer) {
		ct.Write(bytes.Replace(ct.Content(), []byte("f4"), []byte("f4r"), -1))
	}

	tr := NewChain(f1, f2, f3, f4)

	out := new(bytes.Buffer)
	if err := tr.Apply(out, helpers.StringToReader("Test: f4 f3 f1 f2 f1 The End."), []byte("")); err != nil {
		t.Errorf("Multi transformer chain returned an error: %s", err)
	}

	expected := "Test: f4r f3r f1r f2r f1r The End."

	if string(out.Bytes()) != expected {
		t.Errorf("Expected %s got %s", expected, string(out.Bytes()))
	}
}
예제 #17
0
파일: test.go 프로젝트: ellisonch/kinc
func runTest(testName string, fileName string, expected []byte) (passed bool) {
	program := "./" + testName + ".exe"
	fullpath := testName + "/" + fileName + ".aterm"

	cmd := exec.Command(program, fullpath)
	// cmd.Stdin = strings.NewReader("some input")
	var out bytes.Buffer
	var errBytes bytes.Buffer
	cmd.Stdout = &out
	cmd.Stderr = &errBytes
	err := cmd.Run()
	if err != nil {
		s := fmt.Sprintf("Error running command (%s %s):\n", program, fullpath)
		s += "-------STDOUT--------\n"
		s += fmt.Sprintf("%s\n", out.String())
		s += "-------STDERR--------\n"
		s += fmt.Sprintf("%s\n", errBytes.String())
		s += "-------CMDERR--------\n"
		s += fmt.Sprintf("%s\n", err)
		panic(s)
	}

	expectedNoCR := bytes.Replace(expected, []byte{'\r'}, []byte{}, -1)
	actualNoCR := bytes.Replace(out.Bytes(), []byte{'\r'}, []byte{}, -1)

	// if bytes.Compare(actualNoCR, expectedNoCR) != 0 {
	if !bytes.Contains(actualNoCR, expectedNoCR) {
		fmt.Fprintf(os.Stderr, "Suite \"%s\", Test \"%s\"\nExpected:\n%s\nSaw:\n%s\n", testName, fileName, expected, out.Bytes())
		return false
	} else {
		return true
	}
}
예제 #18
0
func RestructureAACFile(file []byte) []byte {
	file = bytes.Replace(file, []byte{9}, []byte{32}, -1)
	file = bytes.Replace(file, []byte{0}, []byte{9}, -1)
	file = bytes.Replace(file, []byte{11}, []byte{32}, -1)
	file = bytes.Replace(file, []byte{3}, []byte{11}, -1)
	return file
}
예제 #19
0
파일: main.go 프로젝트: pombredanne/tools-8
func (i *injectWriter) Write(b []byte) (int, error) {
	//port    := viper.GetInt("ListenPort")
	replace := []byte(
		`	<script type="text/javascript">
			host = (location.hostname || 'localhost');
			document.write('<script type="text/javascript" '
				+ 'src="//' + host + ':35729/livereload.js'
				+ '?mindelay=10&host=' + host + '">'
				+ '</' + 'script>');
		</script>
	</body>`,
	)
	tag := []byte("</body>")

	out := bytes.Replace(b, tag, replace, -1)
	if len(out) == len(b) {
		tag := []byte("</BODY>")
		out = bytes.Replace(b, tag, replace, -1)
	}

	i.h["Content-Length"] = []string{strconv.Itoa(len(out))}
	for k, v := range i.h {
		i.w.Header()[k] = v
	}

	return i.w.Write(out)
}
예제 #20
0
파일: gocco.go 프로젝트: nikhilm/gocco
// `highlight` pipes the source to Pygments, section by section
// delimited by dividerText, then reads back the highlighted output,
// searches for the delimiters and extracts the HTML version of the code
// and documentation for each `Section`
func highlight(source string, sections *list.List) {
	language := getLanguage(source)
	pygments := exec.Command("pygmentize", "-l", language.name, "-f", "html", "-O", "encoding=utf-8")
	pygmentsInput, _ := pygments.StdinPipe()
	pygmentsOutput, _ := pygments.StdoutPipe()
	// start the process before we start piping data to it
	// otherwise the pipe may block
	pygments.Start()
	for e := sections.Front(); e != nil; e = e.Next() {
		pygmentsInput.Write(e.Value.(*Section).codeText)
		if e.Next() != nil {
			io.WriteString(pygmentsInput, language.dividerText)
		}
	}
	pygmentsInput.Close()

	buf := new(bytes.Buffer)
	io.Copy(buf, pygmentsOutput)

	output := buf.Bytes()
	output = bytes.Replace(output, []byte(highlightStart), nil, -1)
	output = bytes.Replace(output, []byte(highlightEnd), nil, -1)

	for e := sections.Front(); e != nil; e = e.Next() {
		index := language.dividerHTML.FindIndex(output)
		if index == nil {
			index = []int{len(output), len(output)}
		}

		fragment := output[0:index[0]]
		output = output[index[1]:]
		e.Value.(*Section).CodeHTML = bytes.Join([][]byte{[]byte(highlightStart), []byte(highlightEnd)}, fragment)
		e.Value.(*Section).DocsHTML = blackfriday.MarkdownCommon(e.Value.(*Section).docsText)
	}
}
예제 #21
0
// escapeControlCharsFromPayload escapes control chars (\n, \t) from a byte slice.
// Context:
// JSON strings are not supposed to contain control characters such as \n, \t,
// ... but some incoming webhooks might still send invalid JSON and we want to
// try to handle that. An example invalid JSON string from an incoming webhook
// might look like this (strings for both "text" and "fallback" attributes are
// invalid JSON strings because they contain unescaped newlines and tabs):
//  `{
//    "text": "this is a test
//						 that contains a newline and tabs",
//    "attachments": [
//      {
//        "fallback": "Required plain-text summary of the attachment
//										that contains a newline and tabs",
//        "color": "#36a64f",
//  			...
//        "text": "Optional text that appears within the attachment
//								 that contains a newline and tabs",
//  			...
//        "thumb_url": "http://example.com/path/to/thumb.png"
//      }
//    ]
//  }`
// This function will search for `"key": "value"` pairs, and escape \n, \t
// from the value.
func escapeControlCharsFromPayload(by []byte) []byte {
	// we'll search for `"text": "..."` or `"fallback": "..."`, ...
	keys := "text|fallback|pretext|author_name|title|value"

	// the regexp reads like this:
	// (?s): this flag let . match \n (default is false)
	// "(keys)": we search for the keys defined above
	// \s*:\s*: followed by 0..n spaces/tabs, a colon then 0..n spaces/tabs
	// ": a double-quote
	// (\\"|[^"])*: any number of times the `\"` string or any char but a double-quote
	// ": a double-quote
	r := `(?s)"(` + keys + `)"\s*:\s*"(\\"|[^"])*"`
	re := regexp.MustCompile(r)

	// the function that will escape \n and \t on the regexp matches
	repl := func(b []byte) []byte {
		if bytes.Contains(b, []byte("\n")) {
			b = bytes.Replace(b, []byte("\n"), []byte("\\n"), -1)
		}
		if bytes.Contains(b, []byte("\t")) {
			b = bytes.Replace(b, []byte("\t"), []byte("\\t"), -1)
		}

		return b
	}

	return re.ReplaceAllFunc(by, repl)
}
예제 #22
0
func wrapPreview(s []byte) []byte {
	if PreviewMode {
		s = bytes.Replace([]byte(PreviewTemplate), []byte("CONTENT"), s, 1)
		s = bytes.Replace(s, []byte("STYLESHEET"), MustAsset("assets/terminal.css"), 1)
	}
	return s
}
예제 #23
0
파일: broker.go 프로젝트: ivanfoo/lazlo
//This stupid hack un-does the utf-escaping performed  by json.Marshal()
//because although Slack correctly parses utf, it doesn't recognize
//utf-escaped markup like <http://myurl.com|myurl>
// UPDATE: I can remove this Once I re-figure-out out how the hell it works
func stupidUTFHack(thingy interface{}) []byte {
	jThingy, _ := json.Marshal(thingy)
	jThingy = bytes.Replace(jThingy, []byte("\\u003c"), []byte("<"), -1)
	jThingy = bytes.Replace(jThingy, []byte("\\u003e"), []byte(">"), -1)
	jThingy = bytes.Replace(jThingy, []byte("\\u0026"), []byte("&"), -1)
	return jThingy
}
예제 #24
0
func (c *cmdSetData) DeleteCmdSet(cmdsetName string) {
	cmds := c.GetCmdSet(cmdsetName)

	inputf, err := os.Open(getFilePath())
	if err != nil && !os.IsExist(err) {
		fmt.Printf("\n%s does not exist in the command sets", cmdsetName)
		return
	}
	defer inputf.Close()

	b, err := ioutil.ReadAll(inputf)
	b = bytes.Replace(b, []byte("--"+cmdsetName+"\n"), []byte(""), 1)
	for _, cmd := range cmds {
		b = bytes.Replace(b, []byte("  "+cmd+"\n"), []byte(""), 1)
	}

	fp := getFilePath()
	f, err := os.Create(fp)
	if err != nil {
		quitWithError("Error writing to file: ", err)
	}
	defer f.Close()

	if _, err = f.Write(b); err != nil {
		quitWithError("Error writing to file: ", err)
	}
}
예제 #25
0
파일: board.go 프로젝트: judwhite/go-sudoku
func loadBoard(b []byte) (*board, error) {
	b = bytes.Replace(b, []byte{'\r'}, []byte{}, -1)
	b = bytes.Replace(b, []byte{'\n'}, []byte{}, -1)
	b = bytes.Replace(b, []byte{' '}, []byte{}, -1)

	if len(b) != 81 {
		return nil, fmt.Errorf("line needs to be 81 chars long. line: %q", string(b))
	}

	board := &board{loading: true}
	for i := 0; i < 81; i++ {
		board.blits[i] = 0x1FF
	}

	for i := 0; i < 81; i++ {
		// allow _ 0 . to indicate an empty cell
		if b[i] != '_' && b[i] != '0' && b[i] != '.' {
			val := uint(b[i] - 48)
			if err := board.SolvePosition(i, val); err != nil {
				return board, err
			}
		}
	}

	board.loading = false

	return board, nil
}
예제 #26
0
func switchTData(w http.ResponseWriter, r *http.Request) {

	lg, lge := loghttp.Logger(w, r)
	_ = lge

	b := fetch.TestData["test.economist.com"]
	sub1 := []byte(`<li><a href="/sections/newcontinent">xxx</a></li>`)

	sub2 := []byte(`<li><a href="/sections/asia">Asia</a></li>`)
	sub3 := []byte(`<li><a href="/sections/asia">Asia</a></li>
		<li><a href="/sections/newcontinent">xxx</a></li>`)

	if bytes.Contains(b, sub1) {
		b = bytes.Replace(b, sub1, []byte{}, -1)
	} else {
		b = bytes.Replace(b, sub2, sub3, -1)
	}

	if bytes.Contains(b, sub1) {
		lg("now contains %s", sub1)
	} else {
		lg("NOT contains %s", sub1)
	}

	fetch.TestData["test.economist.com"] = b

}
예제 #27
0
파일: api.go 프로젝트: joltdb/jolt
func fmtToJsonArr(s []byte) []byte {
	s = bytes.Replace(s, []byte("{"), []byte("[{"), 1)
	s = bytes.Replace(s, []byte("}"), []byte("},"), -1)
	s = bytes.TrimSuffix(s, []byte(","))
	s = append(s, []byte("]")...)
	return s
}
예제 #28
0
func main() {
	templatefile := "template.html"
	csvpath := os.Args[1]
	_, csvfile := filepath.Split(csvpath)

	template, err := ioutil.ReadFile(templatefile)
	if err != nil {
		panic(err)
	}
	csv, err := ioutil.ReadFile(csvpath)
	if err != nil {
		panic(err)
	}
	version, err := ioutil.ReadFile(".version")
	if err != nil {
		panic(err)
	}

	// fmt.Print(string(template)[:10])
	// fmt.Print(string(csv[:10]))

	report := template
	report = bytes.Replace(report, []byte("{csv_here}"), csv, 1)
	report = bytes.Replace(report, []byte("{filename_here}"), []byte(csvfile), 1)
	report = bytes.Replace(report, []byte("{version_here}"), version, 1)

	// fmt.Print(string(report))
	// fmt.Print(reportFileName(csvfile))

	ioutil.WriteFile(reportFileName(csvfile), report, 0644)

	// time.Sleep(10000)
}
예제 #29
0
// PreProcessMarkdown renders full links of commits, issues and pulls to shorter version.
func PreProcessMarkdown(rawHTML []byte, urlPrefix string) []byte {
	ms := commitPattern.FindAll(rawHTML, -1)
	for _, m := range ms {
		m = bytes.TrimSpace(m)
		i := strings.Index(string(m), "commit/")
		j := strings.Index(string(m), "#")
		if j == -1 {
			j = len(m)
		}
		rawHTML = bytes.Replace(rawHTML, m, []byte(fmt.Sprintf(
			` <code><a href="%s">%s</a></code>`, m, ShortSha(string(m[i+7:j])))), -1)
	}
	ms = issueFullPattern.FindAll(rawHTML, -1)
	for _, m := range ms {
		m = bytes.TrimSpace(m)
		i := strings.Index(string(m), "issues/")
		j := strings.Index(string(m), "#")
		if j == -1 {
			j = len(m)
		}
		rawHTML = bytes.Replace(rawHTML, m, []byte(fmt.Sprintf(
			` <a href="%s">#%s</a>`, m, ShortSha(string(m[i+7:j])))), -1)
	}
	return rawHTML
}
예제 #30
0
파일: html_test.go 프로젝트: akavel/vfmd
// simplifyHtml performs a quick & dirty HTML unification in a similar way
// as the fallback approach in the "run_tests" script in testdata dir.
func simplifyHtml(buf []byte) []byte {
	buf = reSimplifyHtml.ReplaceAllLiteral(buf, []byte(">\n<"))
	buf = bytes.Replace(buf, []byte("<pre>\n<code>"), []byte("<pre><code>"), -1)
	buf = bytes.Replace(buf, []byte("</code>\n</pre>"), []byte("</code></pre>"), -1)
	buf = bytes.TrimSpace(buf)
	return buf
}