show.html 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <html lang="zh">
  2. <head>
  3. <title></title>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  6. <script src="/static/vue.js"></script>
  7. <script src="/static/jquery.min.js"></script>
  8. </head>
  9. <body>
  10. <div id="app">
  11. <p v-show="msg!=''">{{msg}}</p>
  12. <div v-if="data!=null">
  13. <!--data-->
  14. <h1>{{data.title}}</h1>
  15. <div v-for="st in data.st">
  16. <h2>{{st.title}}</h2>
  17. <div v-for="ti in st.ti">
  18. <p v-html="ti.title"></p>
  19. <ul>
  20. <li v-for="option in ti.options" v-html="option"></li>
  21. </ul>
  22. <p v-html="'答案:'+ti.answer"></p>
  23. <p v-show="ti.exp!=''" v-html="'题解:'+ti.exp"></p>
  24. </div>
  25. </div>
  26. </div>
  27. </div>
  28. <script>
  29. var URL_PREFIX = "";
  30. var app = new Vue({
  31. el: '#app',
  32. data: {
  33. data: null,
  34. msg: "loading..."
  35. },
  36. methods: {
  37. getContentById() {
  38. var url = location.href.split('/');
  39. var self = this;
  40. $.ajax({
  41. url: URL_PREFIX + "/get/question/content/" + url[url.length - 1],
  42. type: "GET",
  43. success: function(data) {
  44. if (data["success"]) {
  45. data = data["data"];
  46. data = self.encodeHtml(data);
  47. data = self.text2img(data);
  48. data = self.text2tag(data, "上");
  49. data = self.text2tag(data, "下");
  50. data = self.text2tag(data, "划");
  51. self.textFormat(data);
  52. }
  53. else {
  54. self.msg = "获取失败";
  55. }
  56. }
  57. })
  58. },
  59. textFormat(text) {
  60. text = text.split('\n');
  61. var data = {
  62. title: "",
  63. st: []
  64. };
  65. var stCursor = -1,
  66. tiCursor;
  67. var isTitle = -1;
  68. for (var i = 0, j = text.length; i < j; i++) {
  69. if (i === 0) {
  70. data["title"] = text[0];
  71. document.title = "百日题库-"+text[0];
  72. continue;
  73. }
  74. // if (text[i].search(/^[^-~]、|[^~]*.[^~]*单选题|[^~]*.[^~]*每小题/) !== -1) { // 子标题
  75. if (text[i].search(/^[一|二|三|四|五|六|七|八|九|十](.*?)题/) !== -1) { // 子标题
  76. var st = { title: text[i], ti: [] };
  77. data["st"].push(st);
  78. stCursor++;
  79. tiCursor = -1;
  80. continue;
  81. }
  82. if (text[i].search(/^\d+\./) !== -1) { // 题目
  83. var tiTemp = { "title": text[i], options: [], answer: "", exp: "" }
  84. data["st"][stCursor]["ti"].push(tiTemp);
  85. tiCursor++;
  86. isTitle = 1;
  87. continue;
  88. }
  89. if (text[i].search(/^\[图/) !== -1) { // 图片单独一行,直接追加
  90. try {
  91. if (isTitle === 1) {
  92. data["st"][stCursor]["ti"][tiCursor]["title"] += text[i];
  93. }
  94. else if (isTitle === 0) {
  95. data["st"][stCursor]["ti"][tiCursor]["answer"] += text[i];
  96. }
  97. }
  98. catch (e) {
  99. }
  100. continue;
  101. }
  102. if (text[i].search(/^[A-Z]\.[^~]* /) !== -1) { // 题目选项(空格)
  103. var temp = text[i].split(' ');
  104. for (var k = 0, l = temp.length; k < l; k++) {
  105. if (temp[k] !== "") {
  106. data["st"][stCursor]["ti"][tiCursor]['options'].push(temp[k]);
  107. }
  108. }
  109. continue;
  110. }
  111. if (text[i].search(/^[A-Z]\.[^~]*\t/) !== -1) { // 题目选项(tab)
  112. var temp = text[i].split('\t');
  113. for (var k = 0, l = temp.length; k < l; k++) {
  114. if (temp[k] !== "") {
  115. data["st"][stCursor]["ti"][tiCursor]['options'].push(temp[k]);
  116. }
  117. }
  118. continue;
  119. }
  120. if (text[i].search(/^[A-Z]\./) !== -1) { // 题目选项
  121. data["st"][stCursor]["ti"][tiCursor]['options'].push(text[i]);
  122. continue;
  123. }
  124. if (text[i].search(/^(\d)/) !== -1) { // 答案(大括号)
  125. data["st"][stCursor]["ti"][tiCursor]['answer'] += "<br>" + text[i];
  126. continue;
  127. }
  128. if (text[i].search(/^[①|②|③|④|⑤|⑥|⑦|⑧|⑨|⑩|⑪|⑫|⑬|⑭|⑮|⑯|⑰|⑱|⑲|⑳]/) !== -1) { // 答案(圆圈数字)
  129. data["st"][stCursor]["ti"][tiCursor]['answer'] += "<br>" + text[i];
  130. continue;
  131. }
  132. if (text[i].search(/^答案:/) !== -1) { // 答案
  133. data["st"][stCursor]["ti"][tiCursor]['answer'] = text[i].split(':')[1];
  134. isTitle = 0;
  135. continue;
  136. }
  137. if (text[i].search(/^题解:/) !== -1) { // 题解
  138. data["st"][stCursor]["ti"][tiCursor]['exp'] = text[i].split(':')[1];
  139. continue;
  140. }
  141. // 格式外的内容?
  142. try {
  143. if (isTitle === 1) {
  144. data["st"][stCursor]["ti"][tiCursor]["title"] += "<br>" + text[i];
  145. }
  146. else if (isTitle === 0) {
  147. data["st"][stCursor]["ti"][tiCursor]["answer"] += "<br>" + text[i];
  148. }
  149. }
  150. catch (e) {
  151. }
  152. }
  153. console.log(data);
  154. this.data = data;
  155. this.msg = "";
  156. // console.log(JSON.stringify(data));
  157. },
  158. encodeHtml(str) {
  159. var temp = "";
  160. if (str.length == 0) return "";
  161. temp = str.replace(/&/g, "&amp;");
  162. temp = temp.replace(/</g, "&lt;");
  163. temp = temp.replace(/>/g, "&gt;");
  164. temp = temp.replace(/\'/g, "&#39;");
  165. temp = temp.replace(/\"/g, "&quot;");
  166. // 附加
  167. temp = temp.replace(/\\,/g, ", ");
  168. return temp;
  169. },
  170. text2tag(s, name) {
  171. var tag = "";
  172. switch (name) {
  173. case "上":
  174. tag = "sup";
  175. break;
  176. case "下":
  177. tag = "sub";
  178. break;
  179. case "划":
  180. tag = "u";
  181. break;
  182. }
  183. var re = new RegExp(`\\[${name}:(.*?)]`, 'g');
  184. return s.replace(re, `<${tag}>\$1</${tag}>`);
  185. },
  186. text2img(str) {
  187. var url = location.href.split('/');
  188. var contentId = url[url.length - 1];
  189. return str.replace(/\[图:(.*?)\]/g, `<img src="https://sdjrzk-1251357229.cos.ap-guangzhou.myqcloud.com/exam/paper/${contentId}/images/\$1"></img>`)
  190. // https://sdjrzk-1251357229.cos.ap-guangzhou.myqcloud.com/exam/paper/564/images/1810.png
  191. }
  192. },
  193. mounted() {
  194. this.getContentById();
  195. }
  196. });
  197. </script>
  198. </body>
  199. </html>