Turn regex patterns into consts
This commit is contained in:
		
							parent
							
								
									bddb6df567
								
							
						
					
					
						commit
						b03faccd45
					
				| 
						 | 
				
			
			@ -13,6 +13,12 @@ const
 | 
			
		|||
  tco = "https://t.co"
 | 
			
		||||
  nbsp = $Rune(0x000A0)
 | 
			
		||||
 | 
			
		||||
  wwwRegex = re"https?://(www[0-9]?\.)?"
 | 
			
		||||
  manifestRegex = re"(.+(.ts|.m3u8|.vmap))"
 | 
			
		||||
  userpicRegex = re"_(normal|bigger|mini|200x200|400x400)(\.[A-z]+)$"
 | 
			
		||||
  extRegex = re"(\.[A-z]+)$"
 | 
			
		||||
  tombstoneRegex = re"\n* *Learn more"
 | 
			
		||||
 | 
			
		||||
proc stripText*(text: string): string =
 | 
			
		||||
  text.replace(nbsp, " ").strip()
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -25,7 +31,7 @@ proc stripHtml*(text: string): string =
 | 
			
		|||
  html.innerText()
 | 
			
		||||
 | 
			
		||||
proc shortLink*(text: string; length=28): string =
 | 
			
		||||
  result = text.replace(re"https?://(www[0-9]?\.)?", "")
 | 
			
		||||
  result = text.replace(wwwRegex, "")
 | 
			
		||||
  if result.len > length:
 | 
			
		||||
    result = result[0 ..< length] & "…"
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -44,11 +50,11 @@ proc proxifyVideo*(manifest: string; proxy: bool): string =
 | 
			
		|||
  proc cb(m: RegexMatch; s: string): string =
 | 
			
		||||
    result = "https://video.twimg.com" & s[m.group(0)[0]]
 | 
			
		||||
    if proxy: result = getVidUrl(result)
 | 
			
		||||
  result = manifest.replace(re"(.+(.ts|.m3u8|.vmap))", cb)
 | 
			
		||||
  result = manifest.replace(manifestRegex, cb)
 | 
			
		||||
 | 
			
		||||
proc getUserpic*(userpic: string; style=""): string =
 | 
			
		||||
  let pic = userpic.replace(re"_(normal|bigger|mini|200x200|400x400)(\.[A-z]+)$", "$2")
 | 
			
		||||
  pic.replace(re"(\.[A-z]+)$", style & "$1")
 | 
			
		||||
  let pic = userpic.replace(userpicRegex, "$2")
 | 
			
		||||
  pic.replace(extRegex, style & "$1")
 | 
			
		||||
 | 
			
		||||
proc getUserpic*(profile: Profile; style=""): string =
 | 
			
		||||
  getUserPic(profile.userpic, style)
 | 
			
		||||
| 
						 | 
				
			
			@ -86,7 +92,7 @@ proc getLink*(tweet: Tweet | Quote; focus=true): string =
 | 
			
		|||
  if focus: result &= "#m"
 | 
			
		||||
 | 
			
		||||
proc getTombstone*(text: string): string =
 | 
			
		||||
  text.replace(re"\n* *Learn more", "").stripText().strip(chars={' ', '\n'})
 | 
			
		||||
  text.replace(tombstoneRegex, "").stripText().strip(chars={' ', '\n'})
 | 
			
		||||
 | 
			
		||||
proc getTwitterLink*(path: string; params: Table[string, string]): string =
 | 
			
		||||
  let
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,6 +9,7 @@ from htmlgen import a
 | 
			
		|||
const
 | 
			
		||||
  thumbRegex = re".+:url\('([^']+)'\)"
 | 
			
		||||
  gifRegex = re".+thumb/([^\.']+)\.[jpng].*"
 | 
			
		||||
  reColor = re"a:active \{\n +color: (#[A-Z0-9]+)"
 | 
			
		||||
 | 
			
		||||
proc selectAll*(node: XmlNode; selector: string): seq[XmlNode] =
 | 
			
		||||
  if node == nil: return
 | 
			
		||||
| 
						 | 
				
			
			@ -134,7 +135,7 @@ proc getTimelineBanner*(node: XmlNode): string =
 | 
			
		|||
 | 
			
		||||
  let style = node.select("style").innerText()
 | 
			
		||||
  var m: RegexMatch
 | 
			
		||||
  if style.find(re"a:active \{\n +color: (#[A-Z0-9]+)", m):
 | 
			
		||||
  if style.find(reColor, m):
 | 
			
		||||
    return style[m.group(0)[0]]
 | 
			
		||||
 | 
			
		||||
proc getMediaCount*(node: XmlNode): string =
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,6 +9,8 @@ import ../views/general
 | 
			
		|||
export asyncfile, httpclient, os, strutils
 | 
			
		||||
export regex
 | 
			
		||||
 | 
			
		||||
const m3u8Regex* = re"""url="(.+.m3u8)""""
 | 
			
		||||
 | 
			
		||||
proc createMediaRouter*(cfg: Config) =
 | 
			
		||||
  router media:
 | 
			
		||||
    get "/pic/@url":
 | 
			
		||||
| 
						 | 
				
			
			@ -67,7 +69,7 @@ proc createMediaRouter*(cfg: Config) =
 | 
			
		|||
 | 
			
		||||
      if ".vmap" in url:
 | 
			
		||||
        var m: RegexMatch
 | 
			
		||||
        discard content.find(re"""url="(.+.m3u8)"""", m)
 | 
			
		||||
        discard content.find(m3u8Regex, m)
 | 
			
		||||
        url = decodeUrl(content[m.group(0)[0]])
 | 
			
		||||
        content = await safeFetch(url)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue