function write_tex($in, $out_file, $options = array()) { if (!isset($options["contents_only"])) { $options["contents_only"] = false; } if (!isset($options["font_size"])) { $options["font_size"] = "8"; } if (!isset($options["paper_size"])) { $options["paper_size"] = "a4paper"; } if (!isset($options["paper_orientation"])) { $options["paper_orientation"] = ""; } if (!isset($options["max_chars"])) { $options["max_chars"] = ""; } $contents_only = $options["contents_only"]; $font_size = $options["font_size"]; $paper_size = $options["paper_size"]; $paper_orientation = $options["paper_orientation"]; $max_chars = $options["max_chars"]; $force = $options["force"]; if (file_exists($out_file) && !$force) { fputs(STDERR, "Error: already [{$out_file}] exists. use -f option to overwrite it.\n"); return; } if (($out = fopen($out_file, "w")) === false) { fputs(STDERR, "Error: failed to open an output file.\n"); return; } if (!$contents_only) { $documentclass_options = "uplatex"; if ($font_size != "") { $documentclass_options .= ",{$font_size}pt"; } if ($paper_orientation != "") { $documentclass_options .= ",{$paper_orientation}"; } $ans = ""; $ans .= "\\documentclass[{$documentclass_options}]{jsarticle}\n"; $ans .= "\\usepackage[{$paper_size}]{geometry}\n"; $ans .= "\\usepackage[dvipdfmx]{graphicx}\n"; $ans .= "\\usepackage[T1]{fontenc}\n"; $ans .= "\\usepackage{textcomp}\n"; $ans .= "\\usepackage[sc]{mathpazo}\n"; $ans .= "\\usepackage[scaled]{helvet}\n"; $ans .= "\\usepackage{otf}\n"; $ans .= "\\usepackage{longtable}\n"; $ans .= "\\renewcommand{\\ttdefault}{lmtt}\n"; // $ans .= "\\pagestyle{empty}\n"; $ans .= "\\geometry{left=15truemm,right=15truemm,top=20truemm,bottom=20truemm}\n"; $ans .= "\\begin{document}\n"; fputs($out, $ans); } while (($line = fgets($in)) !== false) { // "# ファイル名"はワークシートの先頭行を示す。 if (substr($line, 0, 1) != "#") { continue; } $worksheet_name = trim(substr($line, 1)); $headers = explode(":", "^:" . $options["outheader"] . ":\$"); // ワークシートを出力する。 $row = 0; while (($record = fgetcsv($in, 65536, ",", '"')) !== FALSE) { // 空行はワークシートの最終行を示す。 if ($record == array('')) { break; } $tag = array_shift($record); $num_cols = count($record); switch ($tag) { case "fields": $fields = $record; break; case "types": $types = $record; break; case "params": $params = $record; break; case "titles": $titles = $record; break; case "record": while (($header = array_shift($headers)) !== null) { // ヘッダ情報を出力する。 if ($header == "^") { $table_headers = ""; } else { if ($header == "F") { $table_headers .= "\\hline\n"; for ($col = 0; $col < $num_cols; $col++) { $value = tex_escape_string($fields[$col]); if ($max_chars != "" && $max_chars < mb_strlen($value)) { $value = "\\parbox[t]{{$max_chars}zw}{{$value}}"; } if ($col == 0) { $table_headers .= "\\multicolumn{1}{|c|}{{$value}}\n"; } else { $table_headers .= "&\\multicolumn{1}{c|}{{$value}}\n"; } } $table_headers .= "\\\\\n"; } else { if ($header == "C") { $table_headers .= "\\hline\n"; for ($col = 0; $col < $num_cols; $col++) { $value = tex_escape_string($types[$col]); if ($max_chars != "" && $max_chars < mb_strlen($value)) { $value = "\\parbox[t]{{$max_chars}zw}{{$value}}"; } if ($col == 0) { $table_headers .= "\\multicolumn{1}{|c|}{{$value}}\n"; } else { $table_headers .= "&\\multicolumn{1}{c|}{{$value}}\n"; } } $table_headers .= "\\\\\n"; } else { if ($header == "P") { $table_headers .= "\\hline\n"; for ($col = 0; $col < $num_cols; $col++) { $value = tex_escape_string($params[$col]); if ($max_chars != "" && $max_chars < mb_strlen($value)) { $value = "\\parbox[t]{{$max_chars}zw}{{$value}}"; } if ($col == 0) { $table_headers .= "\\multicolumn{1}{|c|}{{$value}}\n"; } else { $table_headers .= "&\\multicolumn{1}{c|}{{$value}}\n"; } } $table_headers .= "\\\\\n"; } else { if ($header == "T") { $table_headers .= "\\hline\n"; for ($col = 0; $col < $num_cols; $col++) { $value = tex_escape_string($titles[$col]); if ($max_chars != "" && $max_chars < mb_strlen($value)) { $value = "\\parbox[t]{{$max_chars}zw}{{$value}}"; } if ($col == 0) { $table_headers .= "\\multicolumn{1}{|c|}{{$value}}\n"; } else { $table_headers .= "&\\multicolumn{1}{c|}{{$value}}\n"; } } $table_headers .= "\\\\\n"; } else { if ($header == "N") { $table_headers .= "\\hline\n"; for ($col = 0; $col < $num_cols; $col++) { $value = tex_escape_string($params[$col]); if ($max_chars != "" && $max_chars < mb_strlen($value)) { $value = "\\parbox[t]{{$max_chars}zw}{{$value}}"; } if ($col == 0) { $table_headers .= "\\multicolumn{1}{|c|}{{$value}}\n"; } else { $table_headers .= "&\\multicolumn{1}{c|}{{$value}}\n"; } } $table_headers .= "\\\\\n"; } else { if ($header == "\$") { $ans = ""; $ans .= "\\footnotesize\n"; $ans .= "\\begin{longtable}{" . str_repeat("|l", $num_cols) . "|}\n"; // 先頭ページヘッダ $ans .= "\\caption{{$worksheet_name}}\n"; $ans .= "\\\\\n"; $ans .= $table_headers; $ans .= "\\hline\n"; $ans .= "\\hline\n"; $ans .= "\\endfirsthead\n"; // 中間ページヘッダ $ans .= "\\caption[]{{$worksheet_name}(続き)}\n"; $ans .= "\\\\\n"; $ans .= $table_headers; $ans .= "\\hline\n"; $ans .= "\\hline\n"; $ans .= "\\endhead\n"; // 中間ページフッタ $ans .= "\\hline\n"; $ans .= "\\multicolumn{{$num_cols}}{c}{(次ページに続く)}\n"; $ans .= "\\endfoot\n"; // 最終ページフッタ $ans .= "\\hline\n"; $ans .= "\\multicolumn{{$num_cols}}{l}{}\n"; $ans .= "\\endlastfoot\n"; fputs($out, $ans); } } } } } } } } $ans = ""; for ($col = 0; $col < count($record); $col++) { $value = tex_escape_string($record[$col]); if ($max_chars != "" && $max_chars < mb_strlen($value)) { $value = "\\parbox[t]{{$max_chars}zw}{{$value}}"; } if ($col == 0) { $ans .= "{$value}\n"; } else { $ans .= "&{$value}\n"; } } $ans .= "\\\\\n"; fputs($out, $ans); break; default: $contents = implode(",", $record); fputs(STDERR, "Warning: unkown tag[{$tag}]. {$contents}\n"); return; } $row += 1; } $ans = ""; $ans .= "\\hline\n"; $ans .= "\\end{longtable}\n"; $ans .= "\\clearpage\n"; $ans .= "\n"; fputs($out, $ans); } if (!$contents_only) { $ans = ""; $ans .= "\\end{document}\n"; fputs($out, $ans); } return; }
function write_tex($in, $out_file, $options = array()) { if (!isset($options["contents_only"])) { $options["contents_only"] = false; } if (!isset($options["font_size"])) { $options["font_size"] = "8"; } if (!isset($options["paper_size"])) { $options["paper_size"] = "a4paper"; } if (!isset($options["paper_orientation"])) { $options["paper_orientation"] = ""; } if (!isset($options["max_chars"])) { $options["max_chars"] = ""; } if (!isset($options["offset"])) { $options["offset"] = "0"; } $contents_only = $options["contents_only"]; $font_size = $options["font_size"]; $paper_size = $options["paper_size"]; $paper_orientation = $options["paper_orientation"]; $max_chars = $options["max_chars"]; $offset = $options["offset"] + 0; $force = $options["force"]; // データベーススキーマを収集する。 $arguments = array(); while (($line = fgets($in)) !== false) { // "# ワークシート名"はワークシートの先頭行を示す。 if (substr($line, 0, 1) != "#") { continue; } $table = trim(substr($line, 1)); $records = array(); $row = 0; while (($record = fgetcsv($in, 65536, ",", '"')) !== FALSE) { // 空行はワークシートの最終行を示す。 if ($record == array('')) { break; } $tag = array_shift($record); $record = array_slice($record, $offset); $num_cols = count($record); for ($i = 0; $i < $num_cols; $i++) { $record[$i] = tex_escape_string($record[$i]); } switch ($tag) { case "fields": $fields = $record; break; case "types": $types = $record; break; case "params": $params = $record; break; case "titles": $titles = $record; break; case "record": $records[] = $record; break; default: $contents = implode(",", $record); fputs(STDERR, "Warning: Unkown tag[{$tag}]. {$contents}\n"); return; } $row += 1; } $arguments[$table] = ["fields" => $fields, "types" => $types, "params" => $params, "titles" => $titles, "records" => $records, "num_cols" => $num_cols]; } // テンプレートのあるディレクトリと出力ディレクトリを決める。 $template_dirs = array(); $template_dirs[] = "./tex_templates/"; $template_dirs[] = dirname(__FILE__) . "/tex_templates/"; $instance_dirs = array(); $instance_dirs[] = "./tex_output/"; $template_dir = ""; foreach ($template_dirs as $dir) { if (file_exists($dir)) { $template_dir = $dir; break; } } if ($template_dir == "") { fputs(STDERR, "Error: Template directory does not exist.\n"); return; } $instance_dir = ""; foreach ($instance_dirs as $dir) { if (file_exists($dir)) { $instance_dir = $dir; break; } } if ($instance_dir == "") { if (mkdir("./tex_output/", 0700)) { $instance_dir = "./tex_output/"; } else { fputs(STDERR, "Error: Output directory does not exist.\n"); return; } } // コードを生成する。 foreach ($arguments as $table => $detail) { $in_dir = $template_dir; $out_dir = "{$instance_dir}/" . Inflector::camelize($table); if (!file_exists($out_dir)) { mkdir($out_dir); } $in_ext = "tex"; $out_ext = "tex"; $templates = preg_grep("/^\\w+\\.{$in_ext}\$/", scandir($template_dir)); foreach ($templates as $template) { $filename = pathinfo($template, PATHINFO_FILENAME); $in_file = "{$in_dir}/{$filename}.{$in_ext}"; $out_file = "{$out_dir}/{$filename}.{$out_ext}"; $ans = render_html(file_get_contents($in_file), $detail); if (file_exists($out_file) && !$force) { fputs(STDERR, "Error: Already [{$out_file}] exists. Use -f option to override.\n"); return; } file_put_contents($out_file, $ans); } } return; }