| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- <html lang="zh">
- <head>
- <title></title>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
- <script src="/static/vue.js"></script>
- <script src="/static/jquery.min.js"></script>
- </head>
- <body>
- <div id="app">
- <p v-show="msg!=''">{{msg}}</p>
- <div v-if="data!=null">
- <!--data-->
- <h1>{{data.title}}</h1>
- <div v-for="st in data.st">
- <h2>{{st.title}}</h2>
- <div v-for="ti in st.ti">
- <p v-html="ti.title"></p>
- <ul>
- <li v-for="option in ti.options" v-html="option"></li>
- </ul>
- <p v-html="'答案:'+ti.answer"></p>
- <p v-show="ti.exp!=''" v-html="'题解:'+ti.exp"></p>
- </div>
- </div>
- </div>
- </div>
- <script>
- var URL_PREFIX = "";
- var app = new Vue({
- el: '#app',
- data: {
- data: null,
- msg: "loading..."
- },
- methods: {
- getContentById() {
- var url = location.href.split('/');
- var self = this;
- $.ajax({
- url: URL_PREFIX + "/get/question/content/" + url[url.length - 1],
- type: "GET",
- success: function(data) {
- if (data["success"]) {
- data = data["data"];
- data = self.encodeHtml(data);
- data = self.text2img(data);
- data = self.text2tag(data, "上");
- data = self.text2tag(data, "下");
- data = self.text2tag(data, "划");
- self.textFormat(data);
- }
- else {
- self.msg = "获取失败";
- }
- }
- })
- },
- textFormat(text) {
- text = text.split('\n');
- var data = {
- title: "",
- st: []
- };
- var stCursor = -1,
- tiCursor;
- var isTitle = -1;
- for (var i = 0, j = text.length; i < j; i++) {
- if (i === 0) {
- data["title"] = text[0];
- document.title = "百日题库-"+text[0];
- continue;
- }
- // if (text[i].search(/^[^-~]、|[^~]*.[^~]*单选题|[^~]*.[^~]*每小题/) !== -1) { // 子标题
- if (text[i].search(/^[一|二|三|四|五|六|七|八|九|十](.*?)题/) !== -1) { // 子标题
- var st = { title: text[i], ti: [] };
- data["st"].push(st);
- stCursor++;
- tiCursor = -1;
- continue;
- }
- if (text[i].search(/^\d+\./) !== -1) { // 题目
- var tiTemp = { "title": text[i], options: [], answer: "", exp: "" }
- data["st"][stCursor]["ti"].push(tiTemp);
- tiCursor++;
- isTitle = 1;
- continue;
- }
- if (text[i].search(/^\[图/) !== -1) { // 图片单独一行,直接追加
- try {
- if (isTitle === 1) {
- data["st"][stCursor]["ti"][tiCursor]["title"] += text[i];
- }
- else if (isTitle === 0) {
- data["st"][stCursor]["ti"][tiCursor]["answer"] += text[i];
- }
- }
- catch (e) {
- }
- continue;
- }
- if (text[i].search(/^[A-Z]\.[^~]* /) !== -1) { // 题目选项(空格)
- var temp = text[i].split(' ');
- for (var k = 0, l = temp.length; k < l; k++) {
- if (temp[k] !== "") {
- data["st"][stCursor]["ti"][tiCursor]['options'].push(temp[k]);
- }
- }
- continue;
- }
- if (text[i].search(/^[A-Z]\.[^~]*\t/) !== -1) { // 题目选项(tab)
- var temp = text[i].split('\t');
- for (var k = 0, l = temp.length; k < l; k++) {
- if (temp[k] !== "") {
- data["st"][stCursor]["ti"][tiCursor]['options'].push(temp[k]);
- }
- }
- continue;
- }
- if (text[i].search(/^[A-Z]\./) !== -1) { // 题目选项
- data["st"][stCursor]["ti"][tiCursor]['options'].push(text[i]);
- continue;
- }
- if (text[i].search(/^(\d)/) !== -1) { // 答案(大括号)
- data["st"][stCursor]["ti"][tiCursor]['answer'] += "<br>" + text[i];
- continue;
- }
- if (text[i].search(/^[①|②|③|④|⑤|⑥|⑦|⑧|⑨|⑩|⑪|⑫|⑬|⑭|⑮|⑯|⑰|⑱|⑲|⑳]/) !== -1) { // 答案(圆圈数字)
- data["st"][stCursor]["ti"][tiCursor]['answer'] += "<br>" + text[i];
- continue;
- }
- if (text[i].search(/^答案:/) !== -1) { // 答案
- data["st"][stCursor]["ti"][tiCursor]['answer'] = text[i].split(':')[1];
- isTitle = 0;
- continue;
- }
- if (text[i].search(/^题解:/) !== -1) { // 题解
- data["st"][stCursor]["ti"][tiCursor]['exp'] = text[i].split(':')[1];
- continue;
- }
- // 格式外的内容?
- try {
- if (isTitle === 1) {
- data["st"][stCursor]["ti"][tiCursor]["title"] += "<br>" + text[i];
- }
- else if (isTitle === 0) {
- data["st"][stCursor]["ti"][tiCursor]["answer"] += "<br>" + text[i];
- }
- }
- catch (e) {
- }
- }
- console.log(data);
- this.data = data;
- this.msg = "";
- // console.log(JSON.stringify(data));
- },
- encodeHtml(str) {
- var temp = "";
- if (str.length == 0) return "";
- temp = str.replace(/&/g, "&");
- temp = temp.replace(/</g, "<");
- temp = temp.replace(/>/g, ">");
- temp = temp.replace(/\'/g, "'");
- temp = temp.replace(/\"/g, """);
- // 附加
- temp = temp.replace(/\\,/g, ", ");
- return temp;
- },
- text2tag(s, name) {
- var tag = "";
- switch (name) {
- case "上":
- tag = "sup";
- break;
- case "下":
- tag = "sub";
- break;
- case "划":
- tag = "u";
- break;
- }
- var re = new RegExp(`\\[${name}:(.*?)]`, 'g');
- return s.replace(re, `<${tag}>\$1</${tag}>`);
- },
- text2img(str) {
- var url = location.href.split('/');
- var contentId = url[url.length - 1];
- return str.replace(/\[图:(.*?)\]/g, `<img src="https://sdjrzk-1251357229.cos.ap-guangzhou.myqcloud.com/exam/paper/${contentId}/images/\$1"></img>`)
- // https://sdjrzk-1251357229.cos.ap-guangzhou.myqcloud.com/exam/paper/564/images/1810.png
- }
- },
- mounted() {
- this.getContentById();
- }
- });
- </script>
- </body>
- </html>
|