Example #1
1
File: main.go Project: xiam/bridge
func pathName(q string) string {
	var c *regexp.Regexp

	c = regexp.MustCompile(`[A-Z]+`)

	q = c.ReplaceAllStringFunc(q,
		func(part string) string {
			plen := len(part)
			if plen > 2 {
				repl := part[0:1] + strings.ToLower(part[1:plen-1]) + part[plen-1:]
				return repl
			}
			return part
		},
	)

	q = strings.ToLower(q[0:1]) + q[1:len(q)-1] + strings.ToLower(q[len(q)-1:])

	c = regexp.MustCompile(`[A-Z]`)

	q = c.ReplaceAllStringFunc(q,
		func(in string) string {
			return "_" + strings.ToLower(in)
		},
	)

	q = strings.ToLower(q)

	return q
}
Example #2
0
func recGsub(r *regexp.Regexp, in string, f func(string) string) string {
	for keepRunning := true; keepRunning; {
		keepRunning = false
		in = r.ReplaceAllStringFunc(in, func(s string) string {
			keepRunning = true
			return f(s)
		})
	}

	return in
}
Example #3
0
func export(delim *regexp.Regexp, parts ...string) (res string) {
	for _, in := range parts {

		res += delim.ReplaceAllStringFunc(in, func(match string) string {
			switch len(match) {
			case 1:
				return strings.ToUpper(match)
			case 2:
				return strings.ToUpper(match[1:])
			}
			panic("unreachable")
		})
	}

	return
}
Example #4
0
func RegexpReplace(str, replace string, regex *regexp.Regexp, count int) string {
	if 0 == count {
		return str
	}
	if regex != nil {
		if count < 0 {
			return regex.ReplaceAllString(str, replace)
		}
		return regex.ReplaceAllStringFunc(str, func(s string) string {
			if count != 0 {
				count -= 1
				return replace
			}
			return s
		})
	}
	return str
}
Example #5
0
// Create a RegEx for a valid route path
func CreateCompilableRoutePath(route string) (*regexp.Regexp, bool) {
	var re *regexp.Regexp
	var isStatic bool

	regexpString := route

	isStaticRegexp := regexp.MustCompile(`[\(\)\?\<\>:]`)
	if !isStaticRegexp.MatchString(route) {
		isStatic = true
	}

	// Dots
	re = regexp.MustCompile(`([^\\])\.`)
	regexpString = re.ReplaceAllStringFunc(regexpString, func(m string) string {
		return fmt.Sprintf(`%s\.`, string(m[0]))
	})

	// Wildcard names
	re = regexp.MustCompile(`:[^/#?()\.\\]+\*`)
	regexpString = re.ReplaceAllStringFunc(regexpString, func(m string) string {
		return fmt.Sprintf("(?P<%s>.+)", m[1:len(m)-1])
	})

	re = regexp.MustCompile(`:[^/#?()\.\\]+`)
	regexpString = re.ReplaceAllStringFunc(regexpString, func(m string) string {
		return fmt.Sprintf(`(?P<%s>[^/#?]+)`, m[1:len(m)])
	})

	s := fmt.Sprintf(`\A%s\z`, regexpString)

	return regexp.MustCompile(s), isStatic
}
Example #6
0
// 性能更好, 5倍的差距
func (this *NoteService) FixContent(content string, isMarkdown bool) string {
	baseUrl := configService.GetSiteUrl()

	baseUrlPattern := baseUrl

	// 避免https的url
	if baseUrl[0:8] == "https://" {
		baseUrlPattern = strings.Replace(baseUrl, "https://", "https*://", 1)
	} else {
		baseUrlPattern = strings.Replace(baseUrl, "http://", "https*://", 1)
	}

	patterns := []map[string]string{
		map[string]string{"src": "src", "middle": "/file/outputImage", "param": "fileId", "to": "getImage?fileId="},
		map[string]string{"src": "href", "middle": "/attach/download", "param": "attachId", "to": "getAttach?fileId="},
		// 该链接已失效, 不再支持
		map[string]string{"src": "href", "middle": "/attach/downloadAll", "param": "noteId", "to": "getAllAttachs?noteId="},
	}

	for _, eachPattern := range patterns {

		if !isMarkdown {

			// 富文本处理

			// <img src="http://leanote.com/file/outputImage?fileId=5503537b38f4111dcb0000d1">
			// <a href="http://leanote.com/attach/download?attachId=5504243a38f4111dcb00017d"></a>

			var reg *regexp.Regexp
			var reg2 *regexp.Regexp
			if eachPattern["src"] == "src" {
				reg, _ = regexp.Compile("<img(?:[^>]+?)(?:" + eachPattern["src"] + `=['"]*` + baseUrlPattern + eachPattern["middle"] + `\?` + eachPattern["param"] + `=(?:[a-z0-9A-Z]{24})["']*)[^>]*>`)
				reg2, _ = regexp.Compile("<img(?:[^>]+?)(" + eachPattern["src"] + `=['"]*` + baseUrlPattern + eachPattern["middle"] + `\?` + eachPattern["param"] + `=([a-z0-9A-Z]{24})["']*)[^>]*>`)
			} else {
				reg, _ = regexp.Compile("<a(?:[^>]+?)(?:" + eachPattern["src"] + `=['"]*` + baseUrlPattern + eachPattern["middle"] + `\?` + eachPattern["param"] + `=(?:[a-z0-9A-Z]{24})["']*)[^>]*>`)
				reg2, _ = regexp.Compile("<a(?:[^>]+?)(" + eachPattern["src"] + `=['"]*` + baseUrlPattern + eachPattern["middle"] + `\?` + eachPattern["param"] + `=([a-z0-9A-Z]{24})["']*)[^>]*>`)
			}

			content = reg.ReplaceAllStringFunc(content, func(str string) string {
				// str=这样的
				// <img src="http://localhost:9000/file/outputImage?fileId=563d706e99c37b48e0000001" alt="" data-mce-src="http://localhost:9000/file/outputImage?fileId=563d706e99c37b48e0000002">

				eachFind := reg2.FindStringSubmatch(str)
				str = strings.Replace(str,
					eachFind[1],
					eachPattern["src"]+"=\""+baseUrl+"/api/file/"+eachPattern["to"]+eachFind[2]+"\"",
					1)

				// fmt.Println(str)
				return str
			})
			/*
				finds := reg.FindAllStringSubmatch(content, -1) // 查找所有的

				for _, eachFind := range finds {
					if len(eachFind) == 3 {
						// 这一行会非常慢!, content是全部的内容, 多次replace导致
						content = strings.Replace(content,
							eachFind[1],
							eachPattern["src"]+"=\""+baseUrl+"/api/file/"+eachPattern["to"]+eachFind[2]+"\"",
							1)
					}
				}
			*/
		} else {

			// markdown处理
			// ![](http://leanote.com/file/outputImage?fileId=5503537b38f4111dcb0000d1)
			// [selection 2.html](http://leanote.com/attach/download?attachId=5504262638f4111dcb00017f)
			// [all.tar.gz](http://leanote.com/attach/downloadAll?noteId=5503b57d59f81b4eb4000000)

			pre := "!"                        // 默认图片
			if eachPattern["src"] == "href" { // 是attach
				pre = ""
			}

			regImageMarkdown, _ := regexp.Compile(pre + `\[(?:[^]]*?)\]\(` + baseUrlPattern + eachPattern["middle"] + `\?` + eachPattern["param"] + `=(?:[a-z0-9A-Z]{24})\)`)
			regImageMarkdown2, _ := regexp.Compile(pre + `\[([^]]*?)\]\(` + baseUrlPattern + eachPattern["middle"] + `\?` + eachPattern["param"] + `=([a-z0-9A-Z]{24})\)`)

			content = regImageMarkdown.ReplaceAllStringFunc(content, func(str string) string {
				// str=这样的
				// <img src="http://localhost:9000/file/outputImage?fileId=563d706e99c37b48e0000001" alt="" data-mce-src="http://localhost:9000/file/outputImage?fileId=563d706e99c37b48e0000002">

				eachFind := regImageMarkdown2.FindStringSubmatch(str)
				str = strings.Replace(str, eachFind[0], pre+"["+eachFind[1]+"]("+baseUrl+"/api/file/"+eachPattern["to"]+eachFind[2]+")", 1)

				// fmt.Println(str)
				return str
			})

			/*
				findsImageMarkdown := regImageMarkdown.FindAllStringSubmatch(content, -1) // 查找所有的
				// [[![](http://leanote.com/file/outputImage?fileId=5503537b38f4111dcb0000d1) 5503537b38f4111dcb0000d1] [![你好啊, 我很好, 为什么?](http://leanote.com/file/outputImage?fileId=5503537b38f4111dcb0000d1) 5503537b38f4111dcb0000d1]]
				for _, eachFind := range findsImageMarkdown {
					// [![你好啊, 我很好, 为什么?](http://leanote.com/file/outputImage?fileId=5503537b38f4111dcb0000d1) 你好啊, 我很好, 为什么? 5503537b38f4111dcb0000d1]
					if len(eachFind) == 3 {
						content = strings.Replace(content, eachFind[0], pre+"["+eachFind[1]+"]("+baseUrl+"/api/file/"+eachPattern["to"]+eachFind[2]+")", 1)
					}
				}
			*/
		}
	}

	return content
}
Example #7
0
func headlineSynopsis(synopsis, header string, scanner *regexp.Regexp) string {
	return scanner.ReplaceAllStringFunc(synopsis, func(headline string) string {
		return fmt.Sprintf("%s %s", header, headline)
	})
}
Example #8
0
func regexReplaceFunc(r *regexp.Regexp, text string, start string, end string) string {
	return r.ReplaceAllStringFunc(text, func(s string) string {
		return start + r.FindStringSubmatch(s)[1] + end
	})
}