Shortcode

Hugo Shortcode

Hugo提供了便捷的shortcode功能,我们可以简单理解shortcode就是一个Hugo定义的Function,可以根据输入变量Argument,输出我们想要的结果。有下面这两种delimiters的语法,分别是%和<>.

Example

Go 程序语法高亮展示,例子来源于: Hugo Shortcode Section

 
func hIndex(citations []int) int {
	n := len(citations)
	freq := newBit(n + 1)
	for _, v := range citations {
		if v == 0 {
			continue
		}
		if v > n {
			freq.update(n, 1)
		} else {
			freq.update(v, 1)
		}
	}

	total := freq.getSum(n)
	for i := n; i > 0; i-- {
		if total-freq.getSum(i-1) >= i {
			return i
		}
	}

	return 0
}

type bit struct {
	arr []int
}

func newBit(n int) bit {
	arr := make([]int, n+1)
	return bit{
		arr: arr,
	}
}

func (b bit) update(idx, v int) {
	idx++
	for idx < len(b.arr) {
		b.arr[idx] += v
		idx += idx & (-idx)
	}
}

func (b bit) getSum(idx int) int {
	sum := 0
	idx++
	for idx > 0 {
		sum += b.arr[idx]
		idx -= idx & (-idx)
	}
	return sum
}
Avatar
Marco Huang
Yet Another Engineer
comments powered by Disqus

Related