1 /* 2 * Hunt - Hunt is a high-level D Programming Language Web framework that encourages rapid development and clean, pragmatic design. It lets you build high-performance Web applications quickly and easily. 3 * 4 * Copyright (C) 2018-2019 HuntLabs 5 * 6 * Website: www.huntframework.com 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 module hunt.text.FormatterWrapper; 12 13 import std.regex; 14 import std.json; 15 import std.conv; 16 import hunt.logging; 17 18 class FormatterWrapper 19 { 20 private string _open_delimiter; 21 private string _close_delimiter; 22 23 this(string open_delimiter , string close_delimiter) 24 { 25 _open_delimiter = open_delimiter; 26 _close_delimiter = close_delimiter; 27 } 28 29 string[string] match(string input) 30 { 31 ///such as ("\\{\\{\\s*(.+?)\\s*\\}\\}") 32 string[string] result; 33 string pattern; 34 foreach(ch; _open_delimiter) { 35 switch (ch){ 36 case '{': pattern ~= r"\{"; 37 break; 38 case '[': pattern ~= r"\["; 39 break; 40 case '(': pattern ~= r"\("; 41 break; 42 case '}': pattern ~= r"\}"; 43 break; 44 case ']': pattern ~= r"\]"; 45 break; 46 case ')': pattern ~= r"\)"; 47 break; 48 case '$': pattern ~= r"\$"; 49 break; 50 case '*': pattern ~= r"\*"; 51 break; 52 case '+': pattern ~= r"\+"; 53 break; 54 case '^': pattern ~= r"\^"; 55 break; 56 case '?': pattern ~= r"\?"; 57 break; 58 case '|': pattern ~= r"\|"; 59 break; 60 case '=': pattern ~= r"\="; 61 break; 62 case '-': pattern ~= r"\-"; 63 break; 64 case '!': pattern ~= r"\!"; 65 break; 66 case '#': pattern ~= r"\#"; 67 break; 68 default : 69 pattern ~= ch; 70 } 71 } 72 pattern ~= r"\s*(.+?)\s*"; 73 foreach(ch; _close_delimiter) { 74 switch (ch){ 75 case '{': pattern ~= r"\{"; 76 break; 77 case '[': pattern ~= r"\["; 78 break; 79 case '(': pattern ~= r"\("; 80 break; 81 case '}': pattern ~= r"\}"; 82 break; 83 case ']': pattern ~= r"\]"; 84 break; 85 case ')': pattern ~= r"\)"; 86 break; 87 case '$': pattern ~= r"\$"; 88 break; 89 case '*': pattern ~= r"\*"; 90 break; 91 case '+': pattern ~= r"\+"; 92 break; 93 case '^': pattern ~= r"\^"; 94 break; 95 case '?': pattern ~= r"\?"; 96 break; 97 case '|': pattern ~= r"\|"; 98 break; 99 case '=': pattern ~= r"\="; 100 break; 101 case '-': pattern ~= r"\-"; 102 break; 103 case '!': pattern ~= r"\!"; 104 break; 105 case '#': pattern ~= r"\#"; 106 break; 107 default : 108 pattern ~= ch; 109 } 110 } 111 112 auto matchs = matchAll(input , regex(pattern)); 113 foreach(match; matchs) { 114 result[match.captures[0]] = match.captures[1]; 115 } 116 import std.format; 117 // logDebug("format pattern : %s , match result : %s".format(pattern,result)); 118 return result; 119 } 120 121 string format(string input , JSONValue data) 122 { 123 import std.string; 124 auto matchs = match(input); 125 foreach(k , v; matchs) { 126 if(v in data) 127 { 128 if(data[v].type == JSONType..string) 129 input = input.replace(k , data[v].str); 130 else 131 input = input.replace(k , data[v].to!string); 132 } 133 } 134 135 return input; 136 } 137 } 138 139 140 unittest 141 { 142 JSONValue v; 143 v["age"] = 12; 144 v["name"] = "gaoxincheng"; 145 146 string input = "the age is #@$%^&*!+_()[]{} age #@$%^&*!+_()[]{} , and name is #@$%^&*!+_()[]{} name #@$%^&*!+_()[]{} ..."; 147 148 FormatterWrapper wrapper = new FormatterWrapper("#@$%^&*!+_()[]{}","#@$%^&*!+_()[]{}"); 149 assert("the age is 12 , and name is gaoxincheng ..." == wrapper.format(input,v)); 150 }