Skip to content

Commit

Permalink
for web server
Browse files Browse the repository at this point in the history
  • Loading branch information
lymslive committed Jun 22, 2018
1 parent 438f11b commit da0c488
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ tags
*.o
*.out
*.log
*.cgi
core.*
Session.vim
Sessionx.vim
Expand Down
64 changes: 32 additions & 32 deletions content.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,52 @@

## 初稿目录

+ [前言 ](./z/20170816_1.md)
+ [前言 ](z/20170816_1.md)

+ 第一章 VimL 语言主要特点
- [1.1 Hello World 的四种写法 ](./z/20170816_2.md)
- [1.2 同源 ex 命令行 ](./z/20170816_3.md)
- [1.3 弱类型强作用域 ](./z/20170816_4.md)
- [1.4\* 自动加载脚本机制 ](./z/20170816_5.md)
- [1.1 Hello World 的四种写法 ](z/20170816_2.md)
- [1.2 同源 ex 命令行 ](z/20170816_3.md)
- [1.3 弱类型强作用域 ](z/20170816_4.md)
- [1.4\* 自动加载脚本机制 ](z/20170816_5.md)

+ 第二章 VimL 语言基本语法
- [2.1 变量与类型 ](./z/20170817_1.md)
- [2.2 选择与比较 ](./z/20170817_2.md)
- [2.3 循环与迭代 ](./z/20170817_3.md)
- [2.4 函数定义与使用 ](./z/20170817_4.md)
- [2.5\* 异常处理 ](./z/20170817_5.md)
- [2.1 变量与类型 ](z/20170817_1.md)
- [2.2 选择与比较 ](z/20170817_2.md)
- [2.3 循环与迭代 ](z/20170817_3.md)
- [2.4 函数定义与使用 ](z/20170817_4.md)
- [2.5\* 异常处理 ](z/20170817_5.md)

+ 第三章 Vim 常用命令
- [3.1 选项设置 ](./z/20170818_1.md)
- [3.2 快捷键重映射 ](./z/20170818_2.md)
- [3.3 自定义命令 ](./z/20170818_3.md)
- [3.4 execute 与 normal ](./z/20170818_4.md)
- [3.5\* 自动命令与事件 ](./z/20170818_5.md)
- [3.6\* 调试命令 ](./z/20170818_6.md)
- [3.1 选项设置 ](z/20170818_1.md)
- [3.2 快捷键重映射 ](z/20170818_2.md)
- [3.3 自定义命令 ](z/20170818_3.md)
- [3.4 execute 与 normal ](z/20170818_4.md)
- [3.5\* 自动命令与事件 ](z/20170818_5.md)
- [3.6\* 调试命令 ](z/20170818_6.md)

+ 第四章 VimL 数据结构进阶
- [4.1 再谈列表与字符串 ](./z/20170819_1.md)
- [4.2 通用的字典结构 ](./z/20170819_2.md)
- [4.3 嵌套组合与扩展 ](./z/20170819_3.md)
- [4.4\* 正则表达式 ](./z/20170922_1.md)
- [4.1 再谈列表与字符串 ](z/20170819_1.md)
- [4.2 通用的字典结构 ](z/20170819_2.md)
- [4.3 嵌套组合与扩展 ](z/20170819_3.md)
- [4.4\* 正则表达式 ](z/20170922_1.md)

+ 第五章 VimL 函数进阶
- [5.1 可变参数 ](./z/20170819_4.md)
- [5.2 函数引用 ](./z/20170819_5.md)
- [5.3 字典函数 ](./z/20170819_6.md)
- [5.4\* 闭包函数 ](./z/20171023_1.md)
- [5.5 自动函数 ](./z/20171028_1.md)
- [5.1 可变参数 ](z/20170819_4.md)
- [5.2 函数引用 ](z/20170819_5.md)
- [5.3 字典函数 ](z/20170819_6.md)
- [5.4\* 闭包函数 ](z/20171023_1.md)
- [5.5 自动函数 ](z/20171028_1.md)

+ 第六章 VimL 内建函数使用
- [6.1 操作数据类型 ](./z/20170821_1.md)
- [6.2 操作编辑对象 ](./z/20170821_2.md)
- [6.3 操作系统文件 ](./z/20170821_3.md)
- [6.4 其他实用函数 ](./z/20170821_4.md)
- [6.1 操作数据类型 ](z/20170821_1.md)
- [6.2 操作编辑对象 ](z/20170821_2.md)
- [6.3 操作系统文件 ](z/20170821_3.md)
- [6.4 其他实用函数 ](z/20170821_4.md)

+ 第七章 VimL 面向对象编程
- [7.1 面向对象的简介 ](./z/20170821_5.md)
- [7.2 字典即对象 ](./z/20170821_6.md)
- [7.3 自定义类的组织管理 ](./z/20170821_7.md)
- [7.1 面向对象的简介 ](z/20170821_5.md)
- [7.2 字典即对象 ](z/20170821_6.md)
- [7.3 自定义类的组织管理 ](z/20170821_7.md)
<!-- - 7.4\* 面向对象实现示例 20170821_8.md -->

+ 第八章 VimL 异步编程特性
Expand Down
127 changes: 127 additions & 0 deletions vm.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#! /usr/bin/env perl
use strict;
use warnings;
use Text::Markdown qw(markdown);
use FindBin qw($Bin);

my $title = "no title";
my $body = "no body";

# 解析 GET 参数
my $query = $ENV{QUERY_STRING};
my %query = map {$1 => $2 if /(\w+)=(\S+)/} split('&', $query);

my $mdpath = $query{p};
response() unless $mdpath;

$mdpath = "$Bin/$mdpath.md";
response() unless -r $mdpath;

my $mdfile = prefile($mdpath);
my $text = join("", @{$mdfile->{content}});
# my $text = join("\n", @{$mdfile->{content}});
# <pre> will add anthor empty line

$title = $mdfile->{title} if $mdfile->{title};
$body = markdown($text, {tab_width => 2});
fixlink();
# postbody();
response();

# read markdown file, save some information with hash
# Text::Markdown fail to handle code block such as
# ```perl
# any code snippet
# ```
sub prefile
{
my ($filename) = @_;
my $filemark = {content => [], title => '', tags => [], };
my $codeblok = 0;

open(my $fh, '<', $filename) or die "cannot open $filename $!";
# local $/ = undef; $text = <$fh>;
while (<$fh>) {
# chomp;
# title line
if ($. == 1) {
push(@{$filemark->{content}}, $_);
(my $title = $_ ) =~ s/^[#\s]+//;
$filemark->{title} = $title;
next;
}
# tag line
elsif ($. == 2){
my @tags = /`([^`]+)`/g;
if (@tags) {
push(@{$filemark->{tags}}, @tags);
next;
}
}
# begin/end code block ```perl
if (/^\s*```(\S*)\s*$/) {
my $line = $_;
if (!$codeblok) {
$line = qq{<pre><code class="language-$1">};
$codeblok = 1;
}
else {
$line = qq{</code></pre>\n};
$codeblok = 0;
}
push(@{$filemark->{content}}, $line);
}
else {
push(@{$filemark->{content}}, $_);
}
}
close($fh);

return $filemark;
}

# 回应输出
sub response
{
print "Content-type:text/html\n\n";
print <<EndOfHTML;
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<link rel="stylesheet" type="text/css" href="/css/main.css">
<link rel="stylesheet" type="text/css" href="/css/markdown.css">
<title> $title </title>
</head>
<body>
$body
</body>
</html>
EndOfHTML
}

sub debug_query
{
$body = "";
while (my ($key, $value) = each %query) {
$body .= "$key => $value<br/>";
}
}

sub postbody
{
$body .= qq{<hr>\n};
for my $tag (@{$mdfile->{tags}}) {
$body .= qq{<code>$tag</code> };
}
}

sub fixlink
{
my $base = $ENV{SCRIPT_NAME};
if ($mdpath =~ /content\.md$/) {
$body =~ s/href="(.+)\.md"/href="$base?p=$1"/g;
}
}

0 comments on commit da0c488

Please sign in to comment.