Permalink
Browse files

Add line highlighting within code blocks

  • Loading branch information...
1 parent 59edcee commit bad7b1fa0c2003f9237116072768c50437a7b106 @tathamoddie committed Jan 22, 2012
Showing with 37 additions and 1 deletion.
  1. +5 −0 Web/Content/Site.css
  2. +25 −1 Web/Controllers/ContentController.cs
  3. +7 −0 Web/Docs/DNS/Default.md
View
@@ -31,6 +31,11 @@ code {
border-radius: 3px;
}
+ code strong {
+ color: blue;
+ font-weight: normal;
+ }
+
pre {
padding: 5px;
margin: 0 0 0 20px;
@@ -1,5 +1,6 @@
using System;
using System.IO;
+using System.Linq;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;
@@ -23,7 +24,7 @@ public ActionResult Page(string path)
Response.AddHeader("X-File-Path", filePath);
var fileContent = IOFile.ReadAllText(filePath);
- ViewBag.PageContent = new HtmlString(new Markdown().Transform(fileContent));
+ ViewBag.PageContent = ProcessContent(fileContent);
var heading1 = Heading1Regex.Match(fileContent);
ViewBag.Title = heading1.Success ? heading1.Value : "";
@@ -33,6 +34,29 @@ public ActionResult Page(string path)
return View();
}
+ static readonly Regex CodeHighlightRegex = new Regex(@"(?s:(?<=<code>)@@highlight\s(?<highlightOptions>.*?)\n(?<code>.*?)(?=</code>))");
+ static HtmlString ProcessContent(string fileContent)
+ {
+ var content = new Markdown().Transform(fileContent);
+
+ content = CodeHighlightRegex.Replace(content, match =>
+ {
+ var highlightOptions = match.Groups["highlightOptions"].Value;
+ var linesToHighlight = highlightOptions.Split(',').Select(int.Parse);
+ var codeLines = match.Groups["code"].Value.Split('\n');
+ foreach (var lineNumber in linesToHighlight)
+ {
+ var lineIndex = lineNumber - 1;
+ if (lineIndex < 0) throw new ArgumentException(string.Format("Tried to highlight a line with index less than 0. The full code block was:\r\n\r\n{0}", match.Value));
+ if (lineIndex > codeLines.Count() - 1) throw new ArgumentException(string.Format("Tried to highlight a line with index {0}, which doesn't exist. The full code block was:\r\n\r\n{1}", codeLines.Count(), match.Value));
+ codeLines[lineIndex] = string.Format("<strong>{0}</strong>", codeLines[lineIndex]);
+ }
+ return string.Join(Environment.NewLine, codeLines);
+ });
+
+ return new HtmlString(content);
+ }
+
string ResolveFilePath(string docsFolderPath, string path)
{
path = path ?? string.Empty;
View
@@ -10,6 +10,7 @@ Generally, it's the last piece of the deployment puzzle before you can finally s
To query DNS, there's a useful command line tool on both Windows and UNIX called `nslookup`. Launching it without any arguments results in an interactive prompt of its own:
+ @@highlight 1
C:\> nslookup
Default Server: router
Address: 10.0.0.1
@@ -18,6 +19,7 @@ To query DNS, there's a useful command line tool on both Windows and UNIX called
Try typing `github.com` at this new prompt and you'll see something like this:
+ @@highlight 1
> github.com
Server: router
Address: 10.0.0.1
@@ -42,10 +44,12 @@ If we were trying to deliver email to GitHub, we'd need to ask for their *MX (ma
Configure `nslookup` to query for MX records:
+ @@highlight 1
> set type=MX
Then, run another query for `github.com`:
+ @@highlight 1
> github.com
Server: router
Address: 10.0.0.1
@@ -67,6 +71,7 @@ As a web developer, it's likely that you've seen or heard the term *CNAME* befor
Try this lookup:
+ @@highlight 1,2
> set type=CNAME
> longitude.tath.am
Server: router
@@ -89,10 +94,12 @@ There's a common minconception that CNAMEs are slower because they require an ex
You can test this behaviour by clearing your query type:
+ @@highlight 1
> set type=
... then rerunning our last query:
+ @@highlight 1
> longitude.tath.am
Server: router
Address: 10.0.0.1

0 comments on commit bad7b1f

Please sign in to comment.