Last active
March 22, 2018 08:30
-
-
Save rosd89/0bd6a9f55035f2a070719db3d05e4ed5 to your computer and use it in GitHub Desktop.
코드스피츠 S75 2일차 1번 과제 제출
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>75 - 2</title> | |
</head> | |
<body> | |
<img id="a"/> | |
<div id="b"></div> | |
<script type="text/javascript"> | |
const d64 = v => decodeURIComponent( | |
atob(v).split('').map(c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)).join('') | |
); | |
const el = v => document.querySelector(v); | |
const parseMd = v => d64(v).split('\n').map(v => { | |
let i = 3; | |
while (i--) if (v.startsWith('#'.repeat(i + 1))) return `<h${i + 1}>${v.substr(i + 1)}</h${i + 1}>`; | |
return v; | |
}).join('<br>'); | |
const Loader = class { | |
constructor(selector) { | |
this._el = el(selector); | |
} | |
load(v) { | |
this._load(v); | |
} | |
isLoad(ext) { | |
return this._isLoad(ext); | |
} | |
_load(v) { | |
throw 'must be override'; | |
} | |
_isLoad(ext) { | |
throw 'must be override'; | |
} | |
}; | |
const ImageLoader = class extends Loader { | |
constructor(selector) { | |
super(selector); | |
this._router = 'jpg,png,gif'.split(','); | |
} | |
_isLoad(ext) { | |
return this._router.includes(ext) | |
} | |
_load(v) { | |
this._el.src = 'data:text/plain;base64,' + v; | |
} | |
}; | |
const MdLoader = class extends Loader { | |
constructor(selector) { | |
super(selector); | |
this._router = 'md'.split(','); | |
} | |
_isLoad(ext) { | |
return this._router.includes(ext) | |
} | |
_load(v) { | |
this._el.innerHTML = parseMd(v); | |
} | |
}; | |
const Github = class { | |
constructor(id, repo) { | |
this._base = `https://api.github.com/repos/${id}/${repo}/contents/`; | |
} | |
setParser(parser) { | |
this._parser = parser; | |
} | |
load(path) { | |
if (!this._parser) return; | |
else { | |
if (!this._parser.isLoad(path.split('.').pop())) return; | |
} | |
const id = 'callback' + Github._id++; | |
const currentParser = this._parser; | |
const f = Github[id] = ({data: {content}}) => { | |
delete Github[id]; | |
document.head.removeChild(s); | |
currentParser.load(content); | |
}; | |
const s = document.createElement('script'); | |
s.src = `${this._base + path}?callback=Github.${id}`; | |
document.head.appendChild(s); | |
} | |
}; | |
Github._id = 0; | |
const loader = new Github('hikaMaeng', 'codespitz75'); | |
const img = new ImageLoader('#a'); | |
loader.setParser(img); | |
loader.load('einBig.png'); | |
const md = new MdLoader('#b'); | |
loader.setParser(md); | |
loader.load('README.md'); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment