Skip to content

Instantly share code, notes, and snippets.

@kujirahand
Created June 26, 2020 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kujirahand/1161aab8e0e3132035a28b6a5e81eef6 to your computer and use it in GitHub Desktop.
Save kujirahand/1161aab8e0e3132035a28b6a5e81eef6 to your computer and use it in GitHub Desktop.
<!DOCTYPE html><html><meta charset="utf-8"><body>
<style>
textarea {
font-family: "Courier New", Consolas, monospace;
background-color: #f0ffff;
}
</style>
<!-- 入力と出力のテキストボックスを作る -->
<h3>入力 - 以下にタイトルとURLの一覧を貼り付け</h3>
<textarea id="in_ta" rows="8" cols="80">
オンライン結婚式場見学を開始したノバレーゼの成約に向けた取組
https://news.mynavi.jp/article/20200619-1056453/
Windowsユーザーに贈るLinux超入門 第51回
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 = "", subtitle = ""
for (let i in lines) {
let line = lines[i]
// URLかどうか判定
if (line.substr(0, 4) == "http") {
result.push([title, subtitle, line])
// タイトルとサブタイトルを初期化
title = ""
subtitle = ""
continue
}
if (title == "") {
title = line
} else {
subtitle = line
}
}
return result
}
// テキストに変換
function convertText(input) {
const result = []
for (let i in input) {
const line = input[i]
const title = line[0]
const subtitle = line[1]
const url = line[2]
const tag = getTag(url)
const no = pad2(parseInt(i) + 1)
let s = `[${no}] ${tag}${title}\n`
if (subtitle != '') {
s += " " + subtitle + "\n"
}
s += " " + url
result.push(s)
}
return result.join("\n")
}
// URLに応じてタグを付与する
function getTag(url) {
let tag = ''
if (url.match(/article\/[0-9\-]+\//)) {
// タグなし
}
else if (url.match(/article\/[a-zA-Z0-9\-\_]+\//)) {
tag = '【連載】'
}
else if (url.match(/kikaku\//)) {
tag = '【広告】'
}
return tag
}
// スペースを入れて二桁に揃える
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