Hexo-theme-next: 代码染色bug,c++

Created on 30 Jan 2016  ·  5Comments  ·  Source: iissnan/hexo-theme-next

自带代码染色器有bug,c++代码中出现函数指针时会无法染色

代码

template<typename T>
void Mergesort(T *a,int first,int mid,int last,T *temp,bool (*t)(const T &a,const T &b)){
    int i=first,j=mid+1,m=mid,n=last,k=0;
    while(i<=m&&j<=n){
        if(t(a[i],a[j])){
            temp[k++]=a[i++];
        }else{
            temp[k++]=a[j++];
        }
    }
    while(i<=m){
        temp[k++]=a[i++];
    }
    while(j<=n){
        temp[k++]=a[j++];
    }
    for(int i=0;i<k;i++){
        a[first+i]=temp[i];
    }
    return;
}
template<typename T>
void Sort(T *a,int first,int last,T *temp,bool (*t)(const T &a,const T &b)){
    if(first<last){
        int mid=first+(last-first)>>1;
        Sort(a,first,mid,temp,t);
        Sort(a,mid+1,last,temp,t);
        Mergesort(a,first,mid,last,temp,t);
    }
}
template<typename T>
bool mergesort(T *a,int n,bool (*t)(const T &a,const T &b)){
    T *p=new T[n];
    if(p==NULL){
        return false;
    }
    Sort(a,0,n-1,p,t);
    delete[] p;
    return true;
}
Hexo Wontfix

Most helpful comment

Tomorrow 包含 C++ 相关的 css class,因此跟 Tomorrow theme 没有关联。Tomorrow theme 仅根据特定的 class 渲染字符串的颜色,如果输出的代码片段没有带有特定的 class (比如 keyword class),代码不会有颜色。

我尝试使用 highlight.js 本身去渲染这段代码,在指定语言(cpp)的时候,结果是可以被正确渲染(Demo),并且 GitHub 的预览也可以正确的渲染这段代码。也就说,这段代码没有问题,以及 highlight.js 可以渲染这段代码。

那么,问题肯定就出在 Hexo 上了。

从 Hexo 调用 highlight (hexo-util/highlight.js:21) 时执行的结果来看,这段代码并未被 highlight 正确识别,结果如下:

{ relevance: 0,
  value: 'template&lt;typename T&gt;\nvoid Mergesort(T *a,int first,int mid,int last,T *temp,bool (*t)(const T &amp;a,const T &amp;b)){\n    int i=first,j=mid+1,m=mid,n=last,k=0;\n    while(i&lt;=m&amp;&amp;j&lt;=n){\n        if(t(a[i],a[j])){\n            temp[k++]=a[i++];\n        }else{\n            temp[k++]=a[j++];\n        }\n    }\n    while(i&lt;=m){\n        temp[k++]=a[i++];\n    }\n    while(j&lt;=n){\n        temp[k++]=a[j++];\n    }\n    for(int i=0;i&lt;k;i++){\n        a[first+i]=temp[i];\n    }\n    return;\n}\ntemplate&lt;typename T&gt;\nvoid Sort(T *a,int first,int last,T *temp,bool (*t)(const T &amp;a,const T &amp;b)){\n    if(first&lt;last){\n        int mid=first+(last-first)&gt;&gt;1;\n        Sort(a,first,mid,temp,t);\n        Sort(a,mid+1,last,temp,t);\n        Mergesort(a,first,mid,last,temp,t);\n    }\n}\ntemplate&lt;typename T&gt;\nbool mergesort(T *a,int n,bool (*t)(const T &amp;a,const T &amp;b)){\n    T *p=new T[n];\n    if(p==NULL){\n        return false;\n    }\n    Sort(a,0,n-1,p,t);\n    delete[] p;\n    return true;\n}' }

relevance: 0 是指解析的结果与指定的语言之间没有任何关联,也就是说 Hexo highlight 时出现了偏差,具体的偏差在哪里我并未深查。建议去 Hexo 提交 issue。

All 5 comments

听着像是 tomorrow theme 的问题?
https://github.com/chriskempson/tomorrow-theme

应该是tomorrow theme的问题。不知道现在修复了没有。

@shuaitq 代码指定下语言试试

```cpp // You code goes here

依然并没有效果。
我试过了,但是图片现在传不上来

Tomorrow 包含 C++ 相关的 css class,因此跟 Tomorrow theme 没有关联。Tomorrow theme 仅根据特定的 class 渲染字符串的颜色,如果输出的代码片段没有带有特定的 class (比如 keyword class),代码不会有颜色。

我尝试使用 highlight.js 本身去渲染这段代码,在指定语言(cpp)的时候,结果是可以被正确渲染(Demo),并且 GitHub 的预览也可以正确的渲染这段代码。也就说,这段代码没有问题,以及 highlight.js 可以渲染这段代码。

那么,问题肯定就出在 Hexo 上了。

从 Hexo 调用 highlight (hexo-util/highlight.js:21) 时执行的结果来看,这段代码并未被 highlight 正确识别,结果如下:

{ relevance: 0,
  value: 'template&lt;typename T&gt;\nvoid Mergesort(T *a,int first,int mid,int last,T *temp,bool (*t)(const T &amp;a,const T &amp;b)){\n    int i=first,j=mid+1,m=mid,n=last,k=0;\n    while(i&lt;=m&amp;&amp;j&lt;=n){\n        if(t(a[i],a[j])){\n            temp[k++]=a[i++];\n        }else{\n            temp[k++]=a[j++];\n        }\n    }\n    while(i&lt;=m){\n        temp[k++]=a[i++];\n    }\n    while(j&lt;=n){\n        temp[k++]=a[j++];\n    }\n    for(int i=0;i&lt;k;i++){\n        a[first+i]=temp[i];\n    }\n    return;\n}\ntemplate&lt;typename T&gt;\nvoid Sort(T *a,int first,int last,T *temp,bool (*t)(const T &amp;a,const T &amp;b)){\n    if(first&lt;last){\n        int mid=first+(last-first)&gt;&gt;1;\n        Sort(a,first,mid,temp,t);\n        Sort(a,mid+1,last,temp,t);\n        Mergesort(a,first,mid,last,temp,t);\n    }\n}\ntemplate&lt;typename T&gt;\nbool mergesort(T *a,int n,bool (*t)(const T &amp;a,const T &amp;b)){\n    T *p=new T[n];\n    if(p==NULL){\n        return false;\n    }\n    Sort(a,0,n-1,p,t);\n    delete[] p;\n    return true;\n}' }

relevance: 0 是指解析的结果与指定的语言之间没有任何关联,也就是说 Hexo highlight 时出现了偏差,具体的偏差在哪里我并未深查。建议去 Hexo 提交 issue。

Was this page helpful?
0 / 5 - 0 ratings

Related issues

syscca picture syscca  ·  4Comments

DVD0423 picture DVD0423  ·  3Comments

ifgao picture ifgao  ·  4Comments

liyuan989 picture liyuan989  ·  3Comments

keltoy picture keltoy  ·  3Comments