组织模式到DokuWiki转换器

学生

是否有emacs组织模式到dokuwiki转换器?是否有一个dokuwiki到emacs组织模式转换器?

牛奶吐司

我只是在寻找相同的东西,在这里找到了这个问题,还在GitHub上以Perl脚本的形式找到了一个解决方案

脚本

#!/opt/local/bin/perl
#
# Modified by @takaxp
# Last Update: 2012-01-09@22:13
# 2012-01-09: support date([YYYY-MM-DD XX]) insertion
# 2012-01-09: support #+BEGIN_SRC
# 2011-11-16: use strict and warnings
# 2011-11-16: Add conversion from numeric items to `  - '.
# 2011-11-16: Skip headers, if #+TITLE: is, use it as the top headline.
# 2011-11-17: Add DISQUS comment
# 2011-11-30: Add TODO/DONE color in items
# cat hoge.org | perl org2doku.pl > title.txt

# Usage
# 
#   cat <<EOF | ./org2doku
#   * heading
#   
#     - item
#       - sub item
#   
#   ** sub heading
#   EOF           
#   ===== heading =====
#   
#     * item
#       * sub item
#   
#   ==== sub heading ====
#
# Todo
#
#   * font style
# 
# Copyright
#
#   Copyright (c) 2011 Takumi KINJO
#
# License
#
#   The MIT License. See http://www.opensource.org/licenses/mit-license.php
#

use strict;
use warnings;

sub next_item_level {
    my ($len, $level, $last_item_indents) = @_;
    my $index = $#$last_item_indents;
    if ($index >= 0) {
        if ($len == $$last_item_indents[$index]) {
            return $level;
        } else {
            for (my $i = $index; $i >= 0; $i--) {
                my $last_item_indent = $$last_item_indents[$i];
                if ($len > $last_item_indent) {
                    push @$last_item_indents, $len;
                    return $level + 1;
                } elsif ($len == $last_item_indent) {
                    return $level - ($index - $i);
                }
                pop @$last_item_indents;
            }
            return $level;
        }
    } else {
        push @$last_item_indents, $len;
        return $level + 1;
    }
}

sub doku_link {
    my $org_hlink = shift;
    while ($org_hlink =~ s/\[\[(.*?)\]\[(.*?)\]\]/[[$1|$2]]/g) {}
    return $org_hlink;
}

sub shallowest_indent {
    my $last_item_indents = shift;
    my $shallowest_indent = 128;
    foreach my $code_line (@$last_item_indents) {
        if ($code_line =~ /^(\s+).*/) {
            my $len = length($1);
            if ($len < $shallowest_indent) {
                $shallowest_indent = $len;
            }
        } else {
            $shallowest_indent = 0;
        }
    }
    return $shallowest_indent;
}

sub disqus_comment {
    my $title_of_document = shift;
    if($title_of_document){
    print "===== Comments =====\n";
    }else{
    print "====== Comments ======\n";
    }
    print "~~DISQUS~~\n";
}

# Replace TODO in items with colored TODO
sub replace_todo_color {
    my $line = shift;
    $$line =~
    s/TODO/<html><span style="color: red">TODO<\/span><\/html>/;
    $$line =~
    s/DONE/<html><span style="color: ForestGreen">DONE<\/span><\/html>/;
}

my $is_code_block = 0;
my $is_published = 1;
my $is_table = 0;
my $current_block_type = "";
my $item_level = 0;
my $last_head_indent = 0;
my @code_block_buf = ();
my @last_item_indents = ();
my $title_of_document = "";
while (<>) {
    chomp;
    my $line = $_;

    # skip headers
    if($line =~ /^#\+([^\s]+):\s*(.+)/){
    if($1 eq "TITLE"){
        $title_of_document = $2;
        print "====== ".$title_of_document." ======\n";
    }
    next;
    }

    # Replace TAB at the head of lines
    $line =~ s/^\t/<html>&nbsp;&nbsp;&nbsp;&nbsp;<\/html>/;

    if($line =~ /^\[\d\d\d\d-\d\d-\d\d/){
    $line =
        "<html><span style=\"color: #666666\">Date: ".$line."<\/span><\/html>\n";
    }

    # use github style
    $line =~ s/\=(.+?)\=/<html><span style=\"margin:0 2px;padding:2px 5px;white-space:nowrap;border:1px solid #ccc;background-color:#f8f8f8;border-radius:3px;-moz-border-radius:3px;-webkit-border-radius:3px;\">$1<\/span><\/html>/g;

  retry:
    if ($is_published) {
        if (/^=\ (.*)/) {
            # howm header
            $line = $1;
            $line =~ s/<<<//g;
            $line =~ s/^/====== /g;
            $line =~ s/$/ ======/g;
            $item_level = 0;
            @last_item_indents = ();

        } elsif (/^(\*+)\ (.*)/) {
            # org heading
            $last_head_indent = length($1);
            if ($2 =~ /^!/) {
                $is_published = 0;
                next;
            }
        my $max_headline = 6;
        if($title_of_document eq ""){
        $max_headline = 7;
        }
        my $doku_heading = ("=" x ($max_headline - length($1)));
            $line = $doku_heading." $2 ".$doku_heading;
            $item_level = 0;
            @last_item_indents = ();

        } elsif (/^(\s+)[-+*]\ (.*)/) {
            # org item
            $item_level =
        &next_item_level(length($1), $item_level, \@last_item_indents);
        my $content = $2;
        &replace_todo_color(\$content);
            $line = ("  " x $item_level)."* ".$content;

        } elsif (/^\s*(\d+)\.\s*(.+)$/) {
        # numerical item
        $line = "  - ".$2;
            $item_level = 0;
            @last_item_indents = ();

    } elsif (/^#\+begin_src\s(.+)$|#\+begin_example(.*)/i) {
        my $lang = $1;
        my $option = "";
        if($lang =~ /(.+?)\s(.+)$/){
        $lang = $1;
        $option = $2;
        }
        if($lang eq "emacs-lisp"){
        $current_block_type = "lisp";
        }else{
        $current_block_type = $lang;
        }
            # org code block begin
            $item_level = 0;
            @last_item_indents = ();
            $is_code_block = 1;
            next;

        } elsif (/^#\+end_src|^#\+end_example/i) {
            # org code block end
            $item_level = 0;
            @last_item_indents = ();
            $is_code_block = 0;
            print "<code ".$current_block_type.">\n";
            my $shallowest_indent = &shallowest_indent(\@code_block_buf);
            foreach my $line (@code_block_buf) {
                my $regex = "\ " x $shallowest_indent;
                $line =~ s/^$regex//g;
                $line =~ s/^,\*/*/g;
                print $line."\n";
            }
            print "</code>\n";
            @code_block_buf = ();
            next;

        } else {
            # paragraph
            if (!$is_code_block) {
                if ($line =~ /(.*?)\ \*([^\s]+)\*\ (.*)/) {
                    # bold
                    $line = $1." **".$2."** ".$3;
                }
                if ($line =~ /(.*?)\ _([^\s]+)_\ (.*)/) {
                    # under line
                    $line = $1." __".$2."__ ".$3;
                }

        # table
        if($line =~ /[^\|]*\|--/){
            $line = "";
            chomp($line);
        }elsif($line =~ /[^\|]*|/){
            $line =~ s/\|/\^/g;
        }else{
            $line = "?";
        }

            }
            $line =~ s/^\s+//g;
            if ($line) {
                $item_level = 0;
                @last_item_indents = ();
            }
        }

        if ($is_code_block) {
            push @code_block_buf, $_;
        } else {
            print &doku_link($line)."\n";
        }

    } else {
        if (/^(\*+)\ (.*)/) {
            my $head_indent = length($1);
            if (!($2 =~ /^!/)) {
                if ($head_indent <= $last_head_indent) {
                    $is_published = 1;
                    goto retry;
                }
            }
        }
    }
}

&disqus_comment($title_of_document);
1;

我已经尝试转换一个简短的Org文档,并且运行良好。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

组织模式到Markdown转换器?

来自分类Dev

JSON模式到GraphQL模式转换器

来自分类Dev

双向流转换器的设计模式

来自分类Dev

OWL / RDF到UML转换器

来自分类Dev

MDB到JSON转换器

来自分类Dev

Java代码到Swift转换器

来自分类Dev

字到符号转换器

来自分类Dev

MDB到JSON转换器

来自分类Dev

过滤以Mule ESB中的Web服务代理模式传递到转换器的有效负载

来自分类Dev

从SQL到Linq到Fluent Nhibernate的转换器

来自分类Dev

Codeigniter 控制器编号到字转换器

来自分类Dev

WPF-在转换器中检测设计模式

来自分类Dev

面向对象的对象转换器设计模式

来自分类Dev

使用Groovy的CSV到XML转换器

来自分类Dev

HTML图像到JavaScript中的pdf转换器

来自分类Dev

WPF多个枚举标志到转换器参数?

来自分类Dev

像素到基于ppi的dp转换器

来自分类Dev

使用API的XML到CSV转换器

来自分类Dev

转换器参数绑定到viewmodel属性

来自分类Dev

NTLM代理转换器到基本Auth或Oauth

来自分类Dev

Spring Integration CSV文件到POJO转换器

来自分类Dev

Base 10到Base 2转换器

来自分类Dev

MULE Esb xml到Object的转换器

来自分类Dev

使用jspdf的HTML到PDF转换器按钮

来自分类Dev

Windows上的HAML到HTML转换器

来自分类Dev

电话键盘格式的字母到数字转换器

来自分类Dev

WPF:将datagridrow对象传递到转换器

来自分类Dev

图标损坏与Powershell exe到ico转换器

来自分类Dev

球到直角坐标转换器