-
Notifications
You must be signed in to change notification settings - Fork 491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow to add Code Snippets With Syntax Highlighting support (without need for extension) #658
Open
aviade
wants to merge
10
commits into
OpenLiveWriter:master
Choose a base branch
from
aviade:master
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9b7d7df
Include Ribbon.xml in OpenLiveWriter.Ribbon.vcxproj
avezra c3ae7e1
Added InsertCode button to the Insert/Media tab
avezra 1fd94f2
Move InsertCode to dedicated command
avezra 6ad9cf3
Add InsertCode implementation to the Editor class
avezra a3f8426
Add html markup inital
avezra c4fd8cd
Use google's code-prettify
avezra 590f16f
Added prettyprint sctipt to HTML
avezra 3e04be6
Minor style changes
avezra 896e349
Style fixes
avezra 9d2745a
Set default skin to Sunburst
avezra File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,86 @@ | ||
using System; | ||
|
||
namespace OpenLiveWriter.HtmlEditor | ||
{ | ||
public class CodeHighlighter | ||
{ | ||
private const int TAB_SIZE = 4; | ||
|
||
private const string PRE_OPEN = "<pre>"; | ||
private const string PRE_CLOSE = "</pre>"; | ||
private const string PRE_OPEN_STYLED = "<pre class=\"prettyprint\">"; | ||
private const string PRETTIFY_SCRIPT_NAME = "run_prettify.js"; | ||
private const string DEFAULT_SKIN = CodeHighlighterSkins.Sunburst; | ||
|
||
private static readonly string _prettifyScript = | ||
$"<script src=\"https://cdn.rawgit.com/google/code-prettify/master/loader/{PRETTIFY_SCRIPT_NAME}?skin={DEFAULT_SKIN}\"></script>"; | ||
|
||
public static string StyledHtml(string htmlText, string innerHtml) | ||
{ | ||
// Using Google's prettifier (https://github.com/google/code-prettify) | ||
htmlText = SwapTabsForSpaces(htmlText); | ||
htmlText = ReplaceLineEndings(htmlText); | ||
htmlText = RemoveCode(htmlText, PRE_OPEN_STYLED, PRE_CLOSE); | ||
htmlText = RemoveCode(htmlText, PRE_OPEN, PRE_CLOSE); | ||
|
||
bool addScript = ShouldAddJsScript(innerHtml); | ||
string script = addScript ? _prettifyScript : string.Empty; | ||
|
||
string styledHtml = $"{script}{PRE_OPEN_STYLED}{htmlText}{PRE_CLOSE}"; | ||
return styledHtml; | ||
} | ||
|
||
private static string ReplaceLineEndings(string htmlText) | ||
{ | ||
htmlText = htmlText.Replace(Environment.NewLine, ""); | ||
return htmlText; | ||
} | ||
|
||
private static string SwapTabsForSpaces(string text) | ||
{ | ||
return text.Replace("\t", " ".PadLeft(TAB_SIZE)); | ||
} | ||
|
||
private static bool ShouldAddJsScript(string innerHtml) | ||
{ | ||
var body = innerHtml.ToLowerInvariant(); | ||
if (body.Contains(PRETTIFY_SCRIPT_NAME)) | ||
{ | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
private static string RemoveCode(string htmlText, string open, string close) | ||
{ | ||
string htmlL = htmlText.ToLowerInvariant(); | ||
int index = htmlL.IndexOf(open, StringComparison.InvariantCulture); | ||
if (index >= 0) | ||
{ | ||
htmlText = htmlText.Remove(index, open.Length); | ||
htmlL = htmlText.ToLowerInvariant(); | ||
|
||
index = htmlL.IndexOf(close, StringComparison.InvariantCulture); | ||
if (index >= 0) | ||
{ | ||
htmlText = htmlText.Remove(index, close.Length); | ||
} | ||
|
||
} | ||
|
||
return htmlText; | ||
} | ||
} | ||
|
||
// To be used when adding support for skins | ||
// For example: https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js?skin=sunburst | ||
// All Styles: https://rawgit.com/google/code-prettify/master/styles/index.html | ||
public static class CodeHighlighterSkins | ||
{ | ||
public const string Default = "default"; | ||
public const string Sunburst = "sunburst"; | ||
public const string SonsOfObsidian = "sons-of-obsidian"; | ||
public const string Desert = "desert"; | ||
} | ||
} |
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
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
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
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
Binary file added
BIN
+1.1 KB
src/managed/OpenLiveWriter.Localization/CommandBitmaps/InsertCode.LargeImage.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+628 Bytes
src/managed/OpenLiveWriter.Localization/CommandBitmaps/InsertCode.SmallImage.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
20 changes: 20 additions & 0 deletions
20
src/managed/OpenLiveWriter.Localization/Images.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://rawgit.com/ is currently in sunset and will be shut-down by October. Whatever service is used instead, the user should be informed that a third-party script is being inserted into their site, and a third-party service is used to fetch it. Use of subresource integrity would also be desired.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found this site that produces HTML for pretty printing code, and it has an API you can call:
http://hilite.me/
I've not yet worked out what this PR is actually doing, so I haven't any ideas on how to use it to update this PR, but thought I'd mention it in case some one else can use it to update this PR, or else explain what this code is doing so that I can attempt to update it.