public function listAction() { $notes = SqlRepository::getBooknotes(); $booknotes = array(); foreach ($notes as $note) { $infos = array(); $infos['idx_href'] = '/article/list/' . $note->get_index_article_id(); $infos['image_path'] = Repository::findPathFromImages(array('eq' => array('image_id' => $note->get_image_id()))); $title = Repository::findTitleFromArticle(array('eq' => array('article_id' => $note->get_index_article_id()))); $infos['title'] = mb_substr($title, 0, 35); $infos['descs'] = mb_substr($note->get_descs(), 0, 35); $booknotes[] = $infos; } $params = array('infos' => $booknotes, 'title' => '读书笔记', 'category_id' => 2); $this->display(__METHOD__, $params); }
<?php require_once __DIR__ . '/../app/register.php'; LogOpt::init('contents_loader', true); $draft_files = scandir(DRAFT_PATH); foreach ($draft_files as $draft) { if ($draft[0] == '.') { continue; } $article_id = StringOpt::spider_string($draft, 'draft', '.tpl'); if (empty($article_id)) { continue; } $title = Repository::findTitleFromArticle(array('eq' => array('article_id' => $article_id))); if ($title == false) { LogOpt::set('exception', '草稿原文不存在', 'article_id', $article_id); continue; } echo '是否加载该草稿到日志原文?《' . $title . '》' . '(arctile_id:' . $article_id . ') [y/N]'; $sure = fgets(STDIN); if (trim($sure[0]) != 'Y' && trim($sure[0]) != 'y') { continue; } $draft_file = DRAFT_PATH . '/draft' . $article_id . '.tpl'; $infos = array(); $infos['draft'] = file_get_contents($draft_file); $contents = TechlogTools::pre_treat_article($infos['draft']); $indexs = json_encode(TechlogTools::get_index($contents)); if ($indexs != null) { $infos['indexs'] = $indexs; }
public static function pre_treat_article($file) { $font = ''; $lines = explode(PHP_EOL, $file); $contents = ''; $code_id = 'a'; $codes = array(); for ($index = 0; $index < count($lines); ++$index) { $line = $lines[$index]; $line = trim($line); if (empty($line)) { $contents .= '<p> </p>'; } else { if ($line == '<div>') { while (1) { $index++; if ($index >= count($lines)) { break; } $line = trim($lines[$index]); if ($line == '</div>') { break; } $contents .= $line . PHP_EOL; } } else { if ($line == '<table>') { $contents .= '<table class="stdtable" border="1" style="font-size:18;">'; while (1) { $index++; if ($index >= count($lines)) { break; } $line = trim($lines[$index]); if ($line == '</table>') { $contents .= $line; break; } else { if (substr($line, 0, 9) == '<caption>') { $caption = substr($line, 9); $contents .= '<caption' . ' style=\'font-weight:bold;' . ' font-family:"PT Serif","Georgia","Helvetica Neue",Arial,sans-serif;' . ' background-color:#D2E1F0; height:30px;\'>' . $caption . '</caption>'; } else { $tds = explode("\t", $line); if (substr($line, 0, 4) == '<tr>') { $tds[0] = substr($tds[0], 4); $contents .= '<thead>' . '<tr style="background-color:#C5C5C5;">'; if ($tds[0][0] == '[' && $tds[0][strlen($tds[0]) - 1] == ']') { $widths = substr($tds[0], 1, strlen($tds[0]) - 2); $widths = explode(":", $widths); for ($i = 1; $i < count($tds); ++$i) { if (isset($widths[$i - 1])) { $contents .= '<td width="' . $widths[$i - 1] . '%"><strong>' . $tds[$i] . '</strong></td>'; } else { $contents .= '<td><strong>' . $tds[$i] . '</strong></td>'; } } } else { $contents .= '<td><strong>' . implode('</strong></td><td><strong>', $tds) . '</strong></td>'; } $contents .= '</tr></thead>'; } else { $tmp_tds = array(); foreach ($tds as $td) { if ($td !== '') { $tmp_tds[] = $td; } } $contents .= '<tr><td>' . implode('</td><td>', $tmp_tds) . '</td></tr>'; } } } } } else { if ($line == '<ol>' || $line == '<ul>') { $contents .= $line; while (1) { $index++; if ($index >= count($lines)) { break; } $line = trim($lines[$index]); if ($line == '</ol>' || $line == '</ul>') { $contents .= $line; break; } else { $line = self::str_trans($line); if ($font != '') { $line = '<span style="font-family:' . $font . ';">' . $line . '</span>'; } $contents .= '<p><li>' . $line . '</li></p>'; } } } else { if (substr($line, 0, 5) == '<font') { $font = StringOpt::spider_string($line, '<font ', '>'); } else { if ($line == '</font>') { $font = ''; } else { if (substr($line, 0, 4) == '<img') { $id = StringOpt::spider_string($line, 'id="', '"'); if ($id != null) { $image_id = intval(trim($id)); $path = Repository::findPathFromImages(array('eq' => array('image_id' => $image_id))); if ($path != false) { $line = str_replace('id="' . $id . '"', 'src="' . $path . '"', $line); } else { $line = '<strong>图片ID不存在</strong>'; } } else { $path = StringOpt::spider_string($line, 'src="', '"'); } $image_info = GetImageSize(WEB_PATH . '/resource/' . $path); $image_info = $image_info['3']; $width = StringOpt::spider_string($image_info, 'width="', '"'); $width = intval(trim($width)); $contents .= '<p style="text-indent:0em;">' . '<a target="_blank" alt="' . $width . '" href="' . $path . '">' . $line . '</a></p><p> </p>'; } else { if (substr($line, 0, 5) == '<code') { $mode = StringOpt::spider_string($line, 'mode="', '"'); if (empty($mode)) { $mode = 'c_cpp'; } $code = ''; $code_line = 0; $is_php = false; if ($mode === 'php' && $lines[$index + 1] != '<?php') { $is_php = true; } while (1) { $index++; if ($index >= count($lines)) { break; } $line = $lines[$index]; if (trim($line) === '</code>') { break; } $code_wrap = 0; for ($idx = 0; $idx < strlen($line); ++$idx) { if ($line[$idx] == "\t") { $code_wrap += 4; continue; } $value = ord($line[$idx]); if ($value > 127) { $code_wrap++; if ($value >= 192 && $value <= 223) { $idx++; } elseif ($value >= 224 && $value <= 239) { $idx = $idx + 2; } elseif ($value >= 240 && $value <= 247) { $idx = $idx + 3; } } $code_wrap++; } $code_line += floor($code_wrap / 80) + 1; $code .= self::str_trans($line, false) . PHP_EOL; } if ($is_php) { $code = '<?php' . PHP_EOL . $code . '?>'; $code_line += 2; } if ($code_line > 30) { $code_line = 30; } $contents .= '<div id="editor_' . $code_id . '"' . ' style="position: relative;' . ' width: 765px;' . ' height: ' . $code_line . 'px">' . trim($code) . '</div><p> </p>'; $codes[] = array('id' => 'editor_' . $code_id++, 'mode' => $mode); continue; } else { if (substr($line, 0, 4) === '<h1>') { $contents .= '<div class="page-header"><h1 id="' . $code_id++ . '">' . self::str_trans(substr($line, 4)) . '</h1></div>'; } else { if (substr($line, 0, 4) === '<h3>') { $contents .= '<p><h3>' . self::str_trans(substr($line, 4)) . '</h3></p>'; } else { if (substr($line, 0, 2) == '<a') { $id = StringOpt::spider_string($line, 'id="', '"'); $title = Repository::findTitleFromArticle(array('eq' => array('article_id' => $id))); if (!$title) { $title = 'ERROR:加载失败'; } $contents .= '<p><a target="_blank" href="/article/list/' . $id . '">' . $title . '</a></p>'; } else { $line = self::str_trans($line); if ($font != '') { $line = '<span style="font-family:' . $font . ';">' . $line . '</span>'; } $contents .= '<p>' . $line . '</p>'; } } } } } } } } } } } } if (!empty($codes)) { $js_arr = array(); foreach ($codes as $code) { $js_arr[] = '{"id":"' . $code['id'] . '","mode":"' . $code['mode'] . '"}'; } $contents .= '<script>var CODE_DIVS=['; $contents .= implode(',', $js_arr); $contents .= '];</script>'; } return $contents; }