常常使用到各种术语什么的,一个一个解释,其实也就是copy Wikipedia词条上的东西,费时费力不讨好,还不如直接挂个链接完事,但是每次添加链接都是个悲剧的事情,各种数据添加的人都要疯掉了。

一个很简单的方法就是添加一个插件,自动给打上标记的的词条挂上维基外链。

实现

其实就是通过php的preg_replace函数来搞定的。

<?php
/**
 *Plugin Name: Wikipedia_Entry_Inserter
 *Version:     0.1
 *Plugin URI:  http://argcv.com/articles/1675.c
 *Description:  insert wikipedia entry with <wiki> or <wiki:zh>
 *Author:      Yu Jing
 *Author URI:  http://argcv.com
 */

function Wikipedia_Entry_Inserter($outer){
    if(!is_singular()) return $outer;
    $outer = preg_replace("/<wiki>(.+?)<\/wiki>/",
        '<a href="//en.wikipedia.org/wiki/$1" title="$1" target="_blank">$1</a>',
        $outer);
    $outer = preg_replace("/<wiki:zh>(.+?)<\/wiki:zh>/",
        '<a href="//zh.wikipedia.org/wiki/$1" title="$1" target="_blank">$1</a>',
        $outer);
    return $outer;
}

add_filter('the_content', 'Wikipedia_Entry_Inserter');
?>

注:由于插件原因,preg_replace 第一个参数各个字符间的空格需要去掉。

只知道原因是Latex插件的问题,具体怎么搞的,没明白

参考: https://goo.gl/MPa3ob

用法

代码保存为php文档,打包为zip,上传到插件中即可。

用的时候使用<wiki>(英文维基)或者<wiki:zh>(中文维基)包裹词条即可。

效果如下:

PS:

一个方法都用插件来实现,我真是有病

另一个方法

前述是使用了字符串直接匹配方法, 把特定的字符串用正则替换为别的内容. 但是它有个不良之处, 就是规则不好弄, 复杂的逻辑需要更复杂的代码去凑. 在 WordPress 平台下, add_shortcode 是另一种比较良好的方法.

代码如下:

<?php
/**
 *Plugin Name: Wikipedia Entry Inserter 
 *Version:     0.2
 *Plugin URI:  http://argcv.com/articles/1675.c
 *Description:  insert wikipedia entry with [wiki] and [/wiki]
 *Author:      Yu Jing
 *Author URI:  http://argcv.com
 */

function wiki_entries_widget($attr, $content = ""){
  extract(shortcode_atts(array(
    "lang" => 'en',
    "entry" => '',
    'class' => 'wiki_widget'), $attr));
  if($content == ''){
    return '';
  }
  $link = '//'. ($lang == 'zh' ? 'zh' : 'en').'.wikipedia.org/wiki/'. ($entry ? $entry : $content);

  return '<a class="' . $class . '" href="'.$link.'" >'.$content.'</a>';
}

add_shortcode("wiki", "wiki_entries_widget");

这样我们只需要 wiki 一个 short code, 并且可以很容易添加各种选项. 比如


[wiki lang='zh' entry='东风破']周杰伦的歌[/wiki]

我们可以选择语言为 zh, 显示为"周杰伦的歌", 点进去的却是东风破这个词条. 这样的实现会比直接字符串匹配要简单一些. 输出代码如下:

[wiki lang='zh' entry='东风破']周杰伦的歌[/wiki]

效果如下:


Yu

Ideals are like the stars: we never reach them, but like the mariners of the sea, we chart our course by them.

25 Comments

熊猫家族 · March 26, 2013 at 13:11

Google Chrome 23.0.1271.97 Google Chrome 23.0.1271.97 GNU/Linux x64 GNU/Linux x64

非常不错 赞一个

我的名字叫麒 · March 24, 2013 at 17:02

Google Chrome 25.0.1364.172 Google Chrome 25.0.1364.172 Windows 7 x64 Edition Windows 7 x64 Edition

难道不怕维基百科有一天也出墙了

    Yu Jing · March 24, 2013 at 17:30

    Google Chrome 25.0.1364.172 Google Chrome 25.0.1364.172 GNU/Linux x64 GNU/Linux x64

    插件deactivate即可

ghostry · March 23, 2013 at 22:25

Google Chrome 26.0.1410.10 Google Chrome 26.0.1410.10 Windows 7 x64 Edition Windows 7 x64 Edition

博主还不够懒.可以连标记都不用打.直接查到维基有的词和…………………..好吧.貌似很难实现.

    Yu Jing · March 23, 2013 at 22:28

    Google Chrome 25.0.1364.172 Google Chrome 25.0.1364.172 GNU/Linux x64 GNU/Linux x64

    求思路

      ghostry · March 23, 2013 at 23:32

      Google Chrome 26.0.1410.10 Google Chrome 26.0.1410.10 Windows 7 x64 Edition Windows 7 x64 Edition

      ╮(╯_╰)╭打字到一半.发现没思路~

        Yu Jing · March 24, 2013 at 07:36

        Google Chrome 25.0.1364.172 Google Chrome 25.0.1364.172 GNU/Linux x64 GNU/Linux x64

        额。。囧
        查所有词,我想到的两个办法
        一个是设定长度为5(或者其他什么),用这些词逐个拼接到”//en.wikipedia.org/wiki/”后面,然后ping过去,返回码不是404就加上超链接
        这样其实是不靠谱的:
        1. 服务器受不了,我用的那个小服务器很脆弱的,禁不起我这么玩
        2. 维基百科收的词太广了,可能满屏幕的不需要的链接。

        另一个是做个表,决定哪些词是需要挂链接的
        这又有问题,我才懒得做这个表呢╮(╯_╰)╭

花七七 · March 23, 2013 at 14:30

Google Chrome 27.0.1440.0 Google Chrome 27.0.1440.0 Windows 8 x64 Edition Windows 8 x64 Edition

話説能夠直接遍歷所有名詞,然後自動去wiki匹配。。

    yu · March 23, 2013 at 14:55

    Internet Explorer 8.0 Internet Explorer 8.0 Windows XP Windows XP

    可惜它是后于RSS输出的。所以RSS订阅的显示的是未被处理的[wiki]标记

    Yu Jing · March 23, 2013 at 17:56

    Internet Explorer 8.0 Internet Explorer 8.0 Windows XP Windows XP

    额…你是说,搞个列表,将列表中的名词挂上wiki?
    若是指的这个…没必要吧…

花七七 · March 23, 2013 at 14:28

Google Chrome 27.0.1440.0 Google Chrome 27.0.1440.0 Windows 8 x64 Edition Windows 8 x64 Edition

囧、你還真找到了這插件。。。

    yu · March 23, 2013 at 14:53

    Internet Explorer 8.0 Internet Explorer 8.0 Windows XP Windows XP

    干嘛要去找(其实是没找到)
    简单的正则式匹配而已,自己随便手写的

简搜博客 · March 22, 2013 at 10:54

Google Chrome 25.0.1364.97 Google Chrome 25.0.1364.97 Windows 7 Windows 7

就是这样才能出效率呀

    Yu Jing · March 22, 2013 at 14:30

    Google Chrome 25.0.1364.172 Google Chrome 25.0.1364.172 GNU/Linux x64 GNU/Linux x64

    服务器的负载换取个人的方便。
    嗯,就是这个意思

馒头饭madfan · March 21, 2013 at 19:30

Internet Explorer 6.0 Internet Explorer 6.0 Windows XP Windows XP

发现都加上连接了。

花舞花落泪 · March 21, 2013 at 19:16

Opera 12.14 Opera 12.14 Windows 7 Windows 7

不错,很实用!

Leniy · March 21, 2013 at 17:22

Google Chrome 25.0.1364.172 Google Chrome 25.0.1364.172 Windows 7 Windows 7

软工导论、编译原理,好吸引人的名字。我大学的学业只有部分和计算机沾边

    Yu Jing · March 21, 2013 at 17:29

    Google Chrome 25.0.1364.172 Google Chrome 25.0.1364.172 Windows 7 x64 Edition Windows 7 x64 Edition

    导论是所谓工程化做软件项目的–名称比内容更好听。编译原理学完后很有成就感,但过程各种不堪回首…

    我四年就玩过几把单片机,插过几节课面包板,对别的专业各种捣腾仪器的才叫羡慕

      Leniy · March 22, 2013 at 07:51

      Google Chrome 25.0.1364.172 Google Chrome 25.0.1364.172 Windows 7 Windows 7

Leniy · March 21, 2013 at 17:05

Google Chrome 25.0.1364.172 Google Chrome 25.0.1364.172 Windows 7 Windows 7

高手啊
我正则只会(.+?)(.*?)以及w+什么的最简单的。

IT外行表示自学压力山大

Ps:其实写个插件替换字段只是体现了IT人的懒得精髓。
而我这篇文章是为了执行一句sql语句而写了个插件。因为我懒得找到存起来的服务商联系方式去找回我cpanel的密码。

    Yu Jing · March 21, 2013 at 17:13

    Google Chrome 25.0.1364.172 Google Chrome 25.0.1364.172 Windows 7 x64 Edition Windows 7 x64 Edition

    表示就用了1个函数,而且只用了很基本的(.+?)什么的…低手一个

      Leniy · March 21, 2013 at 17:16

      Google Chrome 25.0.1364.172 Google Chrome 25.0.1364.172 Windows 7 Windows 7

      我去年因为有十多个月经常要分析差不多结构的仪器数据,就用python和perl仪器做了个小程序。
      为了临时学下没有接触过的正则,用了几天的时间。

        Yu Jing · March 21, 2013 at 17:20

        Google Chrome 25.0.1364.172 Google Chrome 25.0.1364.172 Windows 7 x64 Edition Windows 7 x64 Edition

        从没有正式学过正则…
        最初是用vi的时候看了几下,后来是学校软工导论之类的,后来编译原理啥的,都是学其他的时候顺带用着的

Yu Jing · March 21, 2013 at 17:03

Google Chrome 25.0.1364.172 Google Chrome 25.0.1364.172 Windows 7 x64 Edition Windows 7 x64 Edition

测试。。。

    Leniy · March 21, 2013 at 17:05

    Google Chrome 25.0.1364.172 Google Chrome 25.0.1364.172 Windows 7 Windows 7

    看来是好了

Leave a Reply to 花七七 Cancel reply

Your email address will not be published. Required fields are marked *