<template>
	<template v-for="(item,index) in htmls" :key="index">
		<view v-if="item.type == 'image'" style="text-align:center;">
			<image :src="file_stor(item.content)" mode="heightFix" style="max-width:100%;"></image>
		</view>
		<view v-else-if="item.type == 'video'" style="text-align:center;">
			<video :src="file_stor(item.content)" style="width:100%;"></video>
		</view>
		<view v-else-if="item.type == 'audio'" style="padding:1em;">
			<ciy-audio :src="file_stor(item.content)"></ciy-audio>
		</view>
		<view v-else v-html="item.content" style="width:100%;position: relative;" :style="{padding:padding}"></view>
	</template>
</template>

<script>
	export default {
		props: {
			md: {
				type: String,
				default: ''
			},
			padding: {
				type: String,
				default: '0 0.5em'
			}
		},
		data() {
			return {
				htmls: []
			};
		},
		watch: {
			md: {
				handler(newD, oldD) {
					this.htmls = this.convert(newD);
				},
				immediate: true
			}
		},
		mounted() {},
		methods: {
			fillmdlines() {

			},
			convert(markdown) {
				// #,!,`,@lcr,|表格
				markdown = markdown || '';
				if (markdown.substring(0, 4) == '[MD]')
					markdown = markdown.substring(4).trim();
				var mds = markdown.split('\n');
				var htmls = [];
				var html = '';
				var ispvs = 0; //0文本，1html，2表格
				var isol = false;
				var titnum = [0, 0, 0, 0, 0];
				var tabalign = {};
				for (var m in mds) {
					var mdline = mds[m].trim();
					if (mdline.length == 0) {
						if (ispvs == 0)
							html += '<br/>';
						if (ispvs == 2) {
							html += '</table></div>';
							ispvs = 0;
						}
						if (isol) {
							isol = false;
							html += '</ol>';
						}
						continue;
					}
					if (mdline[0] == '#') { //标题1/2/3
						if (ispvs == 2)
							html += '</table></div>';
						ispvs = 0;
						if (isol) {
							isol = false;
							html += '</ol>';
						}
						var cr = mdline.substring(1, 2).toLowerCase();
						if (mdline.substring(0, 5) == '#####') {
							oladd(4, titnum);
							if (mdline.substring(5, 6) == '.')
								html += '<h5 class="md-h5">' + olnum(4, titnum, '、') + convertcode(mdline.substring(6)) + '</h5>';
							else if (mdline.substring(5, 6) == ':')
								html += '<div class="md-content">' + olnum(4, titnum, ' ') + convertcode(mdline.substring(6)) + '</div>';
							else
								html += '<h5 class="md-h5">' + convertcode(mdline.substring(5)) + '</h5>';
						} else if (mdline.substring(0, 4) == '####') {
							oladd(3, titnum);
							if (mdline.substring(4, 5) == '.')
								html += '<h4 class="md-h4">' + olnum(3, titnum, '、') + convertcode(mdline.substring(5)) + '</h4>';
							else if (mdline.substring(4, 5) == ':')
								html += '<div class="md-content">' + olnum(3, titnum, ' ') + convertcode(mdline.substring(5)) + '</div>';
							else
								html += '<h4 class="md-h4">' + convertcode(mdline.substring(4)) + '</h4>';
						} else if (mdline.substring(0, 3) == '###') {
							oladd(2, titnum);
							if (mdline.substring(3, 4) == '.')
								html += '<h3 class="md-h3">' + olnum(2, titnum, '、') + convertcode(mdline.substring(4)) + '</h3>';
							else if (mdline.substring(3, 4) == ':')
								html += '<div class="md-content">' + olnum(2, titnum, ' ') + convertcode(mdline.substring(4)) + '</div>';
							else
								html += '<h3 class="md-h3">' + convertcode(mdline.substring(3)) + '</h3>';
						} else if (mdline.substring(0, 2) == '##') {
							oladd(1, titnum);
							if (mdline.substring(2, 3) == '.')
								html += '<h2 class="md-h2">' + olnum(1, titnum, '、') + convertcode(mdline.substring(3)) + '</h2>';
							else if (mdline.substring(2, 3) == ':')
								html += '<div class="md-content">' + olnum(1, titnum, ' ') + convertcode(mdline.substring(3)) + '</div>';
							else
								html += '<h2 class="md-h2">' + convertcode(mdline.substring(2)) + '</h2>';
						} else {
							oladd(0, titnum);
							if (cr == 'c')
								html += '<h1 class="md-h1" style="text-align:center;">' + convertcode(mdline.substring(2)) + '</h1>';
							else if (cr == 'r')
								html += '<h1 class="md-h1" style="text-align:right;">' + convertcode(mdline.substring(2)) + '</h1>';
							else if (cr == '.')
								html += '<h1 class="md-h1">' + olnum(0, titnum, '、') + convertcode(mdline.substring(2)) + '</h1>';
							else if (cr == ':')
								html += '<div class="md-content">' + olnum(0, titnum, ' ') + convertcode(mdline.substring(2)) + '</div>';
							else
								html += '<h1 class="md-h1">' + convertcode(mdline.substring(1)) + '</h1>';
						}
					} else if (mdline[0] == '@') { //c居中，r靠右
						if (ispvs == 2)
							html += '</table></div>';
						ispvs = 0;
						var cr = mdline.substring(1, 2).toLowerCase();
						if (cr == 'c')
							html += '<div class="md-content" style="text-align:center;">' + convertcode(mdline.substring(2)) + '</div>';
						else if (cr == 'r')
							html += '<div class="md-content" style="text-align:right;margin-right:1em;">' + convertcode(mdline.substring(2)) + '</div>';
						else if (mdline[1] == '.') {
							if (!isol) {
								isol = true;
								html += '<ol>';
							}
							html += '<li style="line-height: 1.5em;padding: 0.5em 0;">' + convertcode(mdline.substring(2)) + '</li>';
						} else
							html += '<div class="md-content">' + mdline.substring(1) + '</div>';

					} else if (mdline[0] == '!') { //图片、视频、音频
						if (ispvs == 2)
							html += '</table></div>';
						ispvs = 0;
						var mis = mdline.split('|');
						var url = mis[0];
						url = this.file_stor(url.substring(1));
						var ind = url.indexOf('?');
						var match;
						if (ind > -1) {
							match = url.substring(0, ind).match(/\.([^./]+)$/);
						} else
							match = url.match(/\.([^./]+)$/);
						var exurl = match ? match[1] : '';
						htmls.push({
							type: 'text',
							content: html
						});
						html = '';
						if (exurl == 'mp4' || exurl == 'm3u8') {
							var alt = '';
							if (mis[1])
								alt = ' alt="' + mis[1].replace('"', "") + '"';
							htmls.push({
								type: 'video',
								content: url
							});
						} else if (exurl == 'mp3') {
							var alt = '';
							if (mis[1])
								alt = ' alt="' + mis[1].replace('"', "") + '"';
							htmls.push({
								type: 'audio',
								content: url
							});
						} else {
							var alt = '';
							if (mis[1])
								alt = ' alt="' + mis[1].replace('"', "") + '"';
							htmls.push({
								type: 'image',
								content: url
							});
						}
					} else if (mdline[0] == '|' && mdline.substring(mdline.length - 1) == '|') { //表格
						var trs = mdline.substring(1, mdline.length - 1).split('|');
						if (ispvs == 0) {
							html += '<div class="md-table-div"><table class="md-table"><tr style="font-weight:bold;">';
							for (var i in trs) {
								var align = 'left';
								if (trs[i].substring(trs[i].length - 1) == ':') {
									align = 'right';
									trs[i] = trs[i].substring(0, trs[i].length - 1);
								}
								if (trs[i].substring(0, 1) == ':') {
									align = 'center';
									trs[i] = trs[i].substring(1);
								}
								tabalign[i] = align;
								html += '<td style="text-align:' + align + ';">' + convertcode(trs[i].trim()) + '</td>';
							}
						} else {
							html += '<tr>';
							for (var i in trs) {
								html += '<td style="text-align:' + tabalign[i] + ';">' + convertcode(trs[i].trim()) + '</td>';
							}
						}
						html += '</tr>';
						ispvs = 2;
					} else if (mdline[0] == '<') { //原始HTML
						if (ispvs == 2)
							html += '</table></div>';
						ispvs = 1;
						html += mdline;
					} else {
						if (ispvs == 2)
							html += '</table></div>';
						ispvs = 0;
						html += '<div class="md-content">' + convertcode(mdline) + '</div>';
					}
				}
				if (html) {
					if (ispvs == 2)
						html += '</table></div>';
					if (isol) {
						isol = false;
						html += '</ol>';
					}
					htmls.push({
						type: 'text',
						content: html
					});
				}
				return htmls;

				function oladd(idx, olnum) {
					olnum[idx]++;
					for (var i = idx + 1; i < olnum.length; i++)
						olnum[i] = 0;
				}

				function olnum(idx, olnum, end) {
					for (var i = 0; i <= idx; i++) {
						if (olnum[i] == 0)
							continue;
						var ols = [];
						for (var j = i; j <= idx; j++) {
							ols.push(olnum[j]);
						}
						return ols.join('.') + end;
					}
				}

				function convertcode(md) {
					var bcode = false;
					md = md.replace(/  /g, '　');
					while (true) {
						var ind = md.indexOf('`');
						if (ind == -1)
							break;
						var el = '<span class="md-code">';
						if (bcode) {
							bcode = false;
							el = '</span>';
						} else
							bcode = true;
						md = md.substring(0, ind) + el + md.substring(ind + 1);
					}
					while (true) {
						var ind = md.indexOf('**');
						if (ind == -1)
							break;
						var el = '<span style="font-weight:bold;">';
						if (bcode) {
							bcode = false;
							el = '</span>';
						} else
							bcode = true;
						md = md.substring(0, ind) + el + md.substring(ind + 2);
					}
					if (bcode)
						md += '</span>';
					return md;
				}
			}
		}
	}
</script>

<style scoped>
</style>