「小説家になろう」に投稿した作品について、作者はtext形式のバックアップファイルをダウンロードできます。このファイルはhtmlやjsonといった構造化テキストではないため、そのまま他に転用するには不便な形式です。
単純にバックアップファイルとして保管しておく以外に印刷できれば用途の幅が広がると考え、TeXファイルへ変換するシェルスクリプトを作成しました。ptex2pdfをインストール済みの場合、PDF生成まで自動で進められます。なお、MacOS XのBSD sedを前提に作成したので、GNU sedやPerl等を使う場合と比べ、改行の削除は周りくどい記述になっています。
#!/bin/bash
以下をファイルに保存(例:script.sh)し、下記によりターミナルから起動するとデスクトップに変換済ファイルを作成します。
sh script.sh 対象ファイルパス
#!/bin/bash
LANG=C
FILE=Desktop/`basename $1 .txt`.tex
title=`w='【タイトル】';grep -A 1 $w $1|grep -v $w`
author=`w='【作者名】';grep -A 1 $w $1|grep -v $w`
modified=`w='【最終掲載日時】';grep -A 1 $w $1|grep -v $w|sed 's/ /T/'`+09:00
cat << EOF > $FILE
\documentclass[uplatex,a4paper,11pt]{jsbook}
\usepackage[utf8]{inputenc}
\usepackage[driver=dvipdfm,truedimen,margin=25truemm]{geometry}
{\catcode\`\^^M=\active
\gdef\xobeylines{\catcode\`\^^M\active \def^^M{\par\leavevmode}
\setlength\parindent{0pt}
}
\global\def^^M{\par\leavevmode}}
\usepackage{comment}
\begin{comment}
{
"@context":"https://schema.org",
"@type":"Book",
"@id":"https://ncode.syosetu.com/`w='【Nコード】';grep -A 1 $w $1|grep -v $w|tr '[:upper:]' '[:lower:]'`/",
"name":"$title",
"author":{
"@type":"Person",
"name":"$author",
"sameAs":"https://mypage.syosetu.com/`grep 'ユーザID:.*' $1|sed 's/[^0-9]//g'`/"
},
"dateCreated":"`w='【初回掲載日時】';grep -A 1 $w $1|grep -v $w|sed 's/ /T/'`+09:00",
"dateModified":"$modified",
"genre":"`w='【ジャンル】';grep -A 1 $w $1|grep -v $w`",
"keywords":"`w='【キーワード】';grep -A 1 $w $1|grep -v $w`",
"abstract":"`w='【あらすじ】';sed -n /$w/,'$p' $1|sed -n '/【掲載部分数】/q;p'|sed -e /$w/d -e '/\n/d'`"
}
\end{comment}
\title{$title}
\author{$author}
\date{$modified}
\begin{document}
\maketitle
\xobeylines
`sed -n '/----/,$p' $1|sed -n '/【免責事項】/q;p'|sed '$d'|sed -e ':a' -e 'N' -e '$!ba' -e 's/【サブタイトル】\n/\\\section{/g'|sed -e ':a' -e 'N' -e '$!ba' -e 's/【第.章】\n/\\\chapter{/g'|sed -e '/【本文】/d' -e '/----/d' -e 's/section{.*/&}/g' -e 's/chapter{.*/&}/g'|sed -e ':a' -e 'N' -e '$!ba' -e 's/}\n/}/g'`
\end{document}
EOF
ptex2pdf -u -l -output-directory 'Desktop/' $FILE
\usepackage{comment}
から\end{comment}
まの区間にコメントアウトとして出力します。`\
」はエスケープしています。sed -e ':a' -e 'N' -e '$!ba' -e 's/置換前/置換後/g'
について、:a
はラベル、N
は次の行を読み込ませる命令、$!
は最終行の否定、b
は条件分岐で:a
へ命令を移動させています。つまり、最終行に至るまで置換をループしているコードです。&
」は置換前の文字列を示します。上記について、documentclass
の記載内容を下記に置換すると縦書き二段組にできます。
\documentclass[uplatex,
book,tate,twoside, % 本形式、縦書き、見開き
twocolumn, % 二段組
paper=a4, % 用紙サイズ
fontsize=15.5Q,jafontsize=15.5Q, % フォントサイズ
line_length=34zw, % 一行の文字数 (zw=全角1文字の幅)
number_of_lines=25, % 行数
column_gap=10mm, % 段と段の空白
head_space=14mm] % 天の余白
{jlreq}