Showing file name in Jekyll with Pygments code highlight

ε祈祈猫儿з 提交于 2019-12-14 03:57:29

问题


When highlighting a standard file at a known location I'd like to put the file name in the code block; preferably at the top. A hypothetical example would be

{% highlight apache show_filename=/etc/httpd/conf/httpd.conf %}
.... the file contents ....
{% endhighlight %}

which would then render a code block with a filename prefixed. Is there a way to accomplish this?


回答1:


You can simply use the nice html5 figure tag.

"The HTML Element represents self-contained content, frequently with a caption (figcaption), and is typically referenced as a single unit. While it is related to the main flow, its position is independent of the main flow. Usually this is an image, an illustration, a diagram, a code snippet, or a schema that is referenced in the main text, but that can be moved to another page or to an appendix without affecting the main flow." MDN > html > figure

<figure>
  <figcaption>File: folderName/fileName.rb</figcaption>
  {% highlight ruby %}
  def print_hi(name)
    puts "Hi, #{name}"
  end
  {% endhighlight %}
</figure>



回答2:


Another, a bit nicer solution which will work with kramdown:

>Some/test/xml/file.xml
{:.filename}
{% highlight xml %}
.... the file content ....
{% endhighlight %}

And in you .css file you can make it look nice with your code snippet, for example like this:

.fileName{
  padding: 0px;
  margin: 0px;
  background-color: #292929;
  color: lightgrey;
  border-top-left-radius: 3px;
  border-top-right-radius: 3px;
  margin-bottom: 0px;
  padding-left: 1em;
  font-family: Menlo,Monaco,Consolas,"Courier New",monospace;
  border-left: 0px;    
}

Here you can find more info about this kramdown feature.



来源:https://stackoverflow.com/questions/25881134/showing-file-name-in-jekyll-with-pygments-code-highlight

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!