With the release of Jekyll 3, Jekyll's default syntax highlighter has changed from Pygments to Rouge. However, the basic principles outlined below are still similar.
Because I like everything to be nice and pretty, I wanted to change Jekyll’s default syntax highlighting theme to something a little more attractive. Jekyll uses Pygments for its syntax highlighting, and here is what the default theme looks like while highlighting a block of HTML:
Making Line Numbers More Subtle
One huge thing I can do at the beginning is make the line numbers less in your face. I like that they’re there, because line numbers are often helpful when referencing specific elements, but I don’t want them to compete with the code because you only really want to see them when you actually need them. We can make them a little more subtle by adding the following to our _syntax-highlighting.scss file (which comes in every Jekyll project):
I like to declare every color I use as a variable in a separate sass file because they're easier to update later. I'll define $highlight-linenumbers as #ccc in the next section.
Changing the Colors
Now comes the fun part: customizing the colors! Because I wanted to make this as easy to change as possible, I created new variables for each color in my variables sass file. Here are the values I ended up with:
Syntax highlighting works by applying a span with a specific class around various elements of your code. So in order to change what color the HTML tags look, for example, all you have to do is go into _syntax-highlighting.scss and change the color of class .nt (standing for Name.Tag). To find out which class applies to which piece of code, you can look at the source code for a page with highlighted code on it or you can look at the comments of _syntax-highlighting.scss, which are sometimes enough to clue you in. I kind of went back and forth between the two. Eventually, I ended up with this in _syntax-highlighting.scss:
I wanted to test my new theme on tons of code, even if I didn’t usually use or write about those languages. I really only see myself using HTML, Sass, and YAML, but I wanted to make sure other languages looked great too. I got most of the code snippets from the Octopress syntax highlighting test docs.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>A Tiny Page</title><style type="text/css"><!--p{font-size:15pt;color:#000}--></style></head><!-- real comment --><bodybgcolor="#FFFFFF"text="#000000"link="#0000CC"><script language="javascript"type="text/javascript">functionchangeHeight(h){vartds=document.getElementsByTagName("td");for(vari=0;i<tds.length;i++){tds[i].setAttribute("height",h+"px");}}</script><h1>abc</h1><h2class="heading">def</h2><p>Testing page</p></body></html>
# test python (sample from offlineimap)classExitNotifyThread(Thread):"""This class is designed to alert a "monitor" to the fact that a thread has
exited and to provide for the ability for it to find out why."""defrun(self):globalexitthreads,profiledirself.threadid=thread.get_ident()try:ifnotprofiledir:# normal caseThread.run(self)else:try:importcProfileasprofileexceptImportError:importprofileprof=profile.Profile()try:prof=prof.runctx("Thread.run(self)",globals(),locals())exceptSystemExit:passprof.dump_stats( \
profiledir+"/"+str(self.threadid)+"_"+ \
self.getName()+".prof")except:self.setExitCause('EXCEPTION')ifsys:self.setExitException(sys.exc_info()[1])tb=traceback.format_exc()self.setExitStackTrace(tb)else:self.setExitCause('NORMAL')ifnothasattr(self,'exitmessage'):self.setExitMessage(None)ifexitthreads:exitthreads.put(self,True)defsetExitCause(self,cause):self.exitcause=causedefgetExitCause(self):"""Returns the cause of the exit, one of:
'EXCEPTION' -- the thread aborted because of an exception
'NORMAL' -- normal termination."""returnself.exitcausedefsetExitException(self,exc):self.exitexception=excdefgetExitException(self):"""If getExitCause() is 'EXCEPTION', holds the value from
sys.exc_info()[1] for this exception."""returnself.exitexceptiondefsetExitStackTrace(self,st):self.exitstacktrace=stdefgetExitStackTrace(self):"""If getExitCause() is 'EXCEPTION', returns a string representing
the stack trace for this exception."""returnself.exitstacktracedefsetExitMessage(self,msg):"""Sets the exit message to be fetched by a subsequent call to
getExitMessage. This message may be any object or type except
None."""self.exitmessage=msgdefgetExitMessage(self):"""For any exit cause, returns the message previously set by
a call to setExitMessage(), or None if there was no such message
set."""returnself.exitmessage