Skip to content

Commit

Permalink
Repairing previous damage
Browse files Browse the repository at this point in the history
  • Loading branch information
bleroy committed Nov 30, 2011
1 parent 4ee23d2 commit 040a128
Show file tree
Hide file tree
Showing 10 changed files with 27,508 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.refresh
/App_Data/Index
84 changes: 84 additions & 0 deletions App_Code/MarkdownExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using MarkdownSharp;
using MarkdownWebPages;

namespace App_Code {
public static class MarkdownExtensions {
public static string ProcessIncludes(string content, MarkdownWebPage page) {
return Regex.Replace(content, @"^@include\((?<include>~/.*?)\)", match => MarkdownWebPage.RenderFile(match.Groups["include"].Value, page), RegexOptions.Multiline);
}

public static string ProcessComments(string content, MarkdownWebPage page) {
return Regex.Replace(content, @"^//(.*?)$", match => String.Empty, RegexOptions.Multiline);
}

// First Header | Second Header | Third Header
// ------------- | ------------- | ------------
// Content Cell | Content Cell | Content Cell
// Content Cell | Content Cell | Content Cell
public static string ProcessTables(string content, MarkdownWebPage page) {
return Regex.Replace(content,
@"[\r\n]+((.*?)\s+\|\s+)+(.*?)\s*" +
@"[\r\n]+((-+?)\s+\|\s+)+(-+?)\s*" +
@"[\r\n]+(((.*?)\s+\|\s+)+(.*?)\s*[\r\n]+)+",
match => {
var writer = new StringWriter();
writer.WriteLine("<table><thead><tr>");
foreach (Capture header in match.Groups[1].Captures) {
var headerVal = header.Value.Trim();
writer.WriteLine(" <td>" + headerVal.Substring(0, headerVal.Length - 1) + "</td>");
}
writer.WriteLine(" <td>" + match.Groups[3].Value.Trim() + "</td>");
writer.WriteLine("</tr></thead><tbody>");
var rows = match.Groups[7].Captures;
var cells = match.Groups[9].Captures;
var colCount = cells.Count/rows.Count;
var i = 0;
foreach (Capture row in rows) {
writer.WriteLine(" <tr>");
for (var j = 0; j < colCount; j++ ) {
writer.WriteLine(" <td>" + cells[colCount * i + j] + "</td>");
}
writer.WriteLine(" <td>" + match.Groups[10].Captures[i++] + "</td>");
writer.WriteLine(" </tr>");
}
writer.WriteLine("</tbody></table>");
return writer.ToString();
}
, RegexOptions.Multiline);
}

private static readonly Regex RemoveImagesExpression = new Regex("<img [^>]*>");

public static string RemoveImages(string html) {
return RemoveImagesExpression.Replace(html, "");
}

public static string ExtractSummary(string content) {
// Taking the first two paragraphs
var reader = new StringReader(content);
var result = new StringBuilder();
var line = "";
var paragraphs = 1;
var isEmpty = true;
while(line != null && paragraphs > 0) {
line = reader.ReadLine();
if (string.IsNullOrWhiteSpace(line)) {
if (!isEmpty) {
paragraphs--;
isEmpty = true;
}
else {
isEmpty = false;
}
}
result.AppendLine(line);
}
var markdown = new Markdown();
return RemoveImages(markdown.Transform(result.ToString()));
}
}
}
63 changes: 63 additions & 0 deletions App_Code/SearchHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using SimpleLucene.Impl;

namespace App_Code {
public static class SearchHelpers
{
public const int PageSize = 10;

public static void BuildIndex(HttpContext ctx) {
var indexPath = ctx.Server.MapPath("~/App_Data/Index");
var indexWriter = new DirectoryIndexWriter(new DirectoryInfo(indexPath), true);
var documentPath = ctx.Server.MapPath("~/Documentation");
using (var indexService = new IndexService(indexWriter)) {
indexService.IndexEntities(
Directory.EnumerateFiles(documentPath, "*.markdown", SearchOption.AllDirectories),
f => {
var doc = new Document();
var name = Path.GetFileNameWithoutExtension(f);
var text = File.ReadAllText(f);
doc.Add(new Field("Id", name, Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.Add(new Field("Url", "~/Documentation/" + name, Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.Add(new Field("Title", name.Replace('-', ' '), Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("Text", text, Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("Summary", MarkdownExtensions.ExtractSummary(text), Field.Store.YES, Field.Index.NOT_ANALYZED));
return doc;
});
}
}

public static SearchResults Query(HttpContext ctx, string query, int page = 1) {
var indexPath = ctx.Server.MapPath("~/App_Data/Index");
var indexSearcher = new DirectoryIndexSearcher(new DirectoryInfo(indexPath));
using (var searchService = new SearchService(indexSearcher)) {
var parser = new MultiFieldQueryParser(
Lucene.Net.Util.Version.LUCENE_29,
new[] { "Text" },
new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29));
Query multiQuery = parser.Parse(query);

var result = searchService.SearchIndex(multiQuery);
return new SearchResults {
Documents = result.Results
.Skip(PageSize*(page - 1))
.Take(PageSize)
.Select(d => new SearchResult {
Url = d.Get("Url"),
Title = d.Get("Title"),
Summary = d.Get("Summary")
}),
TotalCount = result.Results.Count()
};
}
}
}
}
8 changes: 8 additions & 0 deletions App_Code/SearchResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace App_Code {
public class SearchResult
{
public virtual string Url { get; set; }
public virtual string Title { get; set; }
public virtual string Summary { get; set; }
}
}
10 changes: 10 additions & 0 deletions App_Code/SearchResults.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using Lucene.Net.Documents;

namespace App_Code {
public class SearchResults
{
public virtual IEnumerable<SearchResult> Documents { get; set; }
public virtual int TotalCount { get; set; }
}
}
Binary file added Bin/Lucene.Net.dll
Binary file not shown.
Binary file added Bin/Lucene.Net.pdb
Binary file not shown.
Loading

0 comments on commit 040a128

Please sign in to comment.