forked from OrchardCMS/OrchardDoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
27,508 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.refresh | ||
/App_Data/Index |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
Oops, something went wrong.