分类: 代码如诗

  • 免插件为wordpress添加代码高亮

    免插件为wordpress添加代码高亮

    免插件为wordpress添加代码高亮

    wordpress上实现代码高亮的插件有很多,但为了提升站点的访问速度,还是想尽量少装一些插件,况且各个插件在不同主题不同编辑器中的展现效果也有差异。
    highlight.js

    image-20220912074714896
    highlight.js的特点:

    • 197种语言和248种风格
    • 自动语言检测
    • 多语言代码高亮显示
    • 可用于node.js
    • 适用于任何标记
    • 兼容任何js框架

    highlight.js可用根据内容自动判断197语言类型并使用对应的高亮风格。

    36种常用语言
    如果是常用的36种语言,可以使用highlight.js推荐的CDN链接。

    cdnjs

    <link rel="stylesheet"href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/styles/default.min.css">
    <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/11.6.0/highlight.min.js"></script>

    jsdelivr

    <link rel="stylesheet"href="//cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.6.0/build/styles/default.min.css">
    <script src="//cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.6.0/build/highlight.min.js"></script>

    unpkg

    <link rel="stylesheet"href="//unpkg.com/@highlightjs/cdn-assets@11.6.0/styles/default.min.css">
    <script src="//unpkg.com/@highlightjs/cdn-assets@11.6.0/highlight.min.js"></script>

    对于这36种之外的,则可以按需进行下载。

    最神奇的是,highlight.js居然连Excel的公式都可以进行高亮显示,而且也是自动识别的:

    =ROUND(((D5+E5+F5)*0.7/174*3*8*11)/12,0)

    压缩包中styles目录下即各种高亮格式,而languages目录中,则是刚刚选择的语言种类。

    下载好压缩包之后,将压缩包解压到网站服务器的highlight目录(需自建)

    之后在<head></head>之间加入:

    <link rel="stylesheet" href="/wp-content/highlight/styles/base16/onedark.min.css">
    <script src="/wp-content/highlight/highlight.min.js"></script>

    /wp-content/highlight具体路径根据实际情况修改一下。

    在<footer></footer>之间加入:

    <script>hljs.initHighlightingOnLoad();</script>
  • 谷歌字体加速

    谷歌字体加速

    谷歌字体加速

    博客的服务器原本就在境外,访问速度非常慢,用的主题和插件里还有很多谷歌字体,更加拖慢了加载速度。在网上找了一些谷歌字体的国内镜像,访问速度都不是很理想,最后发现中国科学技术大学的源还依然可用,而且访问速度也还算理想。

    fonts.gstatic.com       fonts-gstatic.proxy.ustclug.org
    fonts.googleapis.com    fonts.proxy.ustclug.org
    ajax.googleapis.com     ajax.proxy.ustclug.org
    themes.googleusercontent.com    google-themes.proxy.ustclug.org<

    另外还有一些其他的常用镜像库:

    registry-1.docker.io            docker.mirrors.ustc.edu.cn
    packages.elastic.co             elastic.proxy.ustclug.org
    ppa.launchpad.net               launchpad.proxy.ustclug.org
    archive.cloudera.com/cdh5/      cloudera.proxy.ustclug.org/cdh5/
    downloads.lede-project.org      lede.proxy.ustclug.org
    downloads.openwrt.org           openwrt.proxy.ustclug.org
    registry.npmjs.org              npmreg.proxy.ustclug.org
    www.npmjs.com                   npm.proxy.ustclug.org

    包括Gravatar镜像访问速度也不错,刚好一同替换掉:
    image-20220909152247060

    secure.gravatar.com gravatar.proxy.ustclug.org
  • 准备将本站迁移到Microsoft Azure

    准备将本站迁移到Microsoft Azure

    准备将本站迁移到Microsoft Azure

    Microsoft Azure申请了12个月200美元的试用权限,准备把本站由现在腾讯云的硅谷主机迁移到Microsoft Azure的韩国主机上。

    之前在腾讯云上使用的是CentOS 7.9.2009 x86_64,但很久以前,在树莓派和虚拟机里比较常用Ubuntu,所以本次使用Ubuntu+宝塔面板方式,简化环境配置过程:

    wget -O install.sh http://download.bt.cn/install/install-ubuntu_6.0.sh && sudo bash install.sh ed8484bec

    建议初次尝试的小伙伴还是首选CentOS,稳定性和兼容性会更好。

    安装好宝塔后,第一步就给了我一个下马威,看情况应该是服务器没有开放宝塔面板的8888端口导致的。

    在虚拟机的网络设置里,打开8888端口

    刷新页面,出现了熟悉的宝塔面板登录界面。

    一键安装LAMP环境

    可以趁安装的时间去修改一下宝塔面板初始用户名和密码。

    一定要记得把安全入口改掉。默认的太复杂不方便记忆。

  • 优化日志

    优化日志

    优化日志

    • 优化缓存功能——20220827;
    • 优化评论功能,解决部分手机浏览器无法发布评论的问题——20220827;
    • 优化首页部分背景图片,压缩并改为本地存储——20220827;
    • 启用腾讯云CDN——20220831;
    • 删除部分无用插件——20220831;
    • 通过自定义插件向functions.php添加函数——20220902
      • 实现主题升级或更换主题时functions.php内自定义代码不丢失
      • 实现个人说明部分支持HTML代码
    <?php
    /* 
    Plugin Name: 模版函数扩展插件
    Description: 给主题模版函数库“functions.php”文件里添加功能,避免因主题升级或更换主题造成对functions.php文件的代码修改丢失 
    Author: <a href="https://blog.guohao.asia/" >NULL</a>
    */
    // Disables Kses only for textarea saves
    foreach (array('pre_term_description', 'pre_link_description', 'pre_link_notes', 'pre_user_description') as $filter) {
      remove_filter($filter, 'wp_filter_kses');
     }
    
        // Disables Kses only for textarea admin displays
    foreach (array('term_description', 'link_description', 'link_notes', 'user_description') as $filter) {
    remove_filter($filter, 'wp_kses_data');
    }
    ?>
    • 替换前端字体-20220903
    *:not([class*="icon"]):not(i) {
    font-family: Segoe UI, "Microsoft Yahei" !important;
    }
    • 修改首页元素显示条件,提升PC端及移动端显示效果-20220903
    • 通过自定义插件向functions.php添加函数,解除大尺寸图片上传限制——20220905
    //禁用WordPress大图片(2560大小)裁剪功能
    add_filter( 'big_image_size_threshold', '__return_false' );
    • 替换代码高亮为highlight.js-20220912
    <link rel="stylesheet" href="/wp-content/highlight/styles/base16/onedark.min.css">		<!--引入代码深色背景-->
    <script src="/wp-content/highlight/highlight.min.js"></script>							<!--引入代码高亮js-->
    <script src="/wp-content/highlight/highlightjs-line-numbers/dist/highlightjs-line-numbers.min.js"></script>			<!--引入添加行号js-->
    <script>
    	hljs.initHighlightingOnLoad();
    	hljs.initLineNumbersOnLoad();
    </script>
    • 增加首行缩进
    //WordPress 文章首行缩进
    function Bing_paragraph_indentation( $content ){
        return str_replace( '<p', '<p style="text-indent:2em;"', $content );
    }
    add_filter( 'the_content', 'Bing_paragraph_indentation' );
  • 根据类别列数据,将不同类别拆分成独立excel文件

    根据类别列数据,将不同类别拆分成独立excel文件

    根据类别列数据,将不同类别拆分成独立excel文件

    Sub 根据类别列数据,将不同类别拆分成独立excel文件()
        Dim d As Object, arr, brr, r, kr, i&, j&, k&, x&, Mystr$
        Dim Rng As Range, Rg As Range, tRow&, tCol&, aCol&, pd&, mypath$
        Dim Cll As Range, sht As Worksheet '第一部分,用户选择保存分表工作簿的路径。
            With Application.FileDialog(msoFileDialogFolderPicker)
            '选择保存工作薄的文件路径
                .AllowMultiSelect = False
                '不允许多选
                If .Show Then
                   mypath = .SelectedItems(1)
                    '读取选择的文件路径
                  Else
                 Exit Sub
    '如果没有选择保存路径,则退出程序
                  End If
            End With
    
            If Right(mypath, 1) <> "\" Then mypath = mypath & "\"
            ' '
            '第二部分遍历总表数据,通过字典将指定字段的不同明细行过滤保存
                Set d = CreateObject("scripting.dictionary") 'set字典
                Set Rg = Application.InputBox("请框选拆分依据列!只能选择单列单元格区域!", Title:="提示", Type:=8)
                '用户选择的拆分依据列
                tCol = Rg.Column '取拆分依据列列标
                tRow = Val(Application.InputBox("请输入总表标题行的行数?"))
                '用户设置总表的标题行数
                If tRow < 0 Then MsgBox "标题行数不能为负数,程序退出。": Exit Sub
                    Set Rng = ActiveSheet.UsedRange '总表的数据区域
                    Set Cll = ActiveSheet.Cells '用于在分表粘贴和总表同样行高列宽的数据格式
                    arr = Rng '数据范围装入数组arr
                    tCol = tCol - Rng.Column + 1 '计算依据列在数组中的位置
                    aCol = UBound(arr, 2) '数据源的列数
                For i = tRow + 1 To UBound(arr) '遍历数组arr
                    If arr(i, tCol) = "" Then arr(i, tCol) = "单元格空白"
                        Mystr = arr(i, tCol) '统一转换为字符串格式
                        If Not d.exists(Mystr) Then
                            d(Mystr) = i '字典中不存在关键词则将行号装入字典
                        Else
                            d(Mystr) = d(Mystr) & "," & i '如果存在则合并行号,以逗号间隔
                End If
            Next
    ' '
    '第三部分遍历字典取出分表数据明细,建立不同工作簿保存数据。
        Application.ScreenUpdating = False '关闭屏幕刷新
        Application.DisplayAlerts = False '关闭系统警告信息
        kr = d.keys '字典的key集
        For i = 0 To UBound(kr) '遍历字典key值
            If kr(i) <> "" Then '如果key不为空
                r = Split(d(kr(i)), ",") '取出item里储存的行号
                ReDim brr(1 To UBound(r) + 1, 1 To aCol) '声明放置结果的数组brr
                k = 0
                For x = 0 To UBound(r)
                    k = k + 1'累加记录行数
                    For j = 1 To aCol '遍历读取列
                        brr(k, j) = arr(r(x), j)
                     Next
                 Next
                    With Workbooks.Add
                '新建一个工作簿
                        With .Sheets(1).[a1]
                            Cll.Copy '复制粘贴总表的单元格格式
                            .PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
                            Cells.NumberFormat = "@" '设置文本格式,防止文本值变形
                            If tRow > 0 Then .Resize(tRow, aCol) = arr '放标题行
                                .Offset(tRow, 0).Resize(k, aCol) = brr '放置数据区域
                                .Select'激活A1单元格
                        End With
            .SaveAs mypath & kr(i), xlWorkbookDefault '保存工作簿
            .Close True '关闭工作簿
                    End With
            End If
        Next
    '收尾巴。
    Set d = Nothing '释放字典
    Erase arr: Erase brr '释放数组
    MsgBox "处理完成。", , "提醒"
    Application.ScreenUpdating = True '恢复屏幕刷新
    Application.DisplayAlerts = True '恢复显示系统警告和消息
    End Sub
    
AI 助手