Created
June 26, 2020 02:02
-
-
Save kujirahand/2cecd8302ad13da16ad01e6a56021fb3 to your computer and use it in GitHub Desktop.
This file contains 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><meta charset="utf-8"><body> | |
<!-- 入力と出力のテキストボックスを作る --> | |
<h3>入力 - 以下にタイトルとURLの一覧を貼り付け</h3> | |
<textarea id="in_ta" rows="8" cols="80"> | |
オンライン結婚式場見学を開始したノバレーゼの成約に向けた取組 | |
https://news.mynavi.jp/article/20200619-1056453/ | |
Windows 10にUbuntu 20.04 LTSをインストールする | |
https://news.mynavi.jp/article/liunx_win-51/ | |
ノートPCと周辺機器の連係が一気に楽になる「ドッキングステーション」 | |
https://news.mynavi.jp/kikaku/beginner_pc-3/ | |
</textarea> | |
<h3>出力</h3> | |
<textarea id="out_ta" rows="8" cols="80"></textarea> | |
<!-- 整形プログラム --> | |
<script> | |
// 入力と出力のテキストボックスのDOMを取得 | |
const in_ta = document.querySelector("#in_ta") | |
const out_ta = document.querySelector("#out_ta") | |
in_ta.onkeydown = convertTextFromTA // イベント設定 | |
// 整形処理を実行 | |
function convertTextFromTA() { | |
// 入力を得る | |
const lines = getInputArray(in_ta.value) | |
// 出力を生成 | |
out_ta.value = convertText(lines) | |
} | |
convertTextFromTA() | |
// 入力データを配列形式で得る | |
function getInputArray(text) { | |
// 前後の空白をトリム | |
text = text.replace(/^\s+/, '').replace(/\s+$/, '') | |
// 改行コードを揃えて一行ずつに分解 | |
const lines = text.replace(/\r\n/g, "\n").split("\n") | |
// タイトルとURLの組に分ける | |
const result = [] | |
let title = "", url = "" | |
for (let i in lines) { | |
let line = lines[i] | |
if (i % 2 == 0) { | |
title = line | |
continue | |
} | |
url = line | |
result.push([title, url]) | |
} | |
return result | |
} | |
// テキストに変換 | |
function convertText(input) { | |
const result = [] | |
for (let i in input) { | |
const line = input[i] | |
const title = line[0] | |
const url = " " + line[1] | |
const no = pad2(parseInt(i) + 1) | |
result.push(`[${no}] ${title}\n${url}`) | |
} | |
return result.join("\n") | |
} | |
// スペースを入れて二桁に揃える | |
function pad2(s) { | |
const n = " " + s | |
return n.substr(n.length - 2, 2) | |
} | |
</script> | |
</body></html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment