Skip to content

Commit

Permalink
Add filter capability to settings file (manual update) to render with…
Browse files Browse the repository at this point in the history
… external programs
  • Loading branch information
VinsWorldcom committed Jun 18, 2021
2 parents ad90096 + 7c95e7f commit 1325fcc
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 62 deletions.
91 changes: 83 additions & 8 deletions NppMarkdownPanel/MarkdownPanelController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using NppMarkdownPanel.Forms;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
Expand Down Expand Up @@ -39,6 +40,12 @@ public class MarkdownPanelController
private bool syncViewWithCaretPosition;
private bool syncViewWithScrollPosition;

private const int FILTERS = 10;
private string[] filterExts = new string[FILTERS];
private string[] filterProgs = new string[FILTERS];
private string[] filterArgs = new string[FILTERS];
private int filterFound = 0;

public MarkdownPanelController()
{
scintillaGateway = new ScintillaGateway(PluginBase.GetCurrentScintilla());
Expand All @@ -53,8 +60,11 @@ public void OnNotification(ScNotification notification)
{
if (isPanelVisible)
{
if (notification.Header.Code == (uint)SciMsg.SCN_UPDATEUI && (ValidateMkdnExtension() || ValidateHtmlExtension()))
if (notification.Header.Code == (uint)SciMsg.SCN_UPDATEUI)
{
if ( ! (ValidateMkdnExtension() || ValidateHtmlExtension()) )
return;

var firstVisible = scintillaGateway.GetFirstVisibleLine();
var buffer = scintillaGateway.LinesOnScreen()/2;
var lastLine = scintillaGateway.GetLineCount();
Expand Down Expand Up @@ -89,8 +99,7 @@ public void OnNotification(ScNotification notification)
}
}
}
else
if (notification.Header.Code == (uint)NppMsg.NPPN_BUFFERACTIVATED)
else if (notification.Header.Code == (uint)NppMsg.NPPN_BUFFERACTIVATED)
{
UpdateEditorInformation();
RenderMarkdown();
Expand All @@ -102,10 +111,17 @@ public void OnNotification(ScNotification notification)
// // Any modifications made ?
// if (isInsert || isDelete)
// {
if ( ValidateMkdnExtension() || ValidateHtmlExtension() )
{
lastTickCount = Environment.TickCount;
RenderMarkdown();
}
// }
}
else if (notification.Header.Code == (uint)NppMsg.NPPN_FILESAVED)
{
RenderMarkdown();
}
}
}

Expand All @@ -114,6 +130,8 @@ private bool ValidateMkdnExtension()
StringBuilder sbFileExtension = new StringBuilder(Win32.MAX_PATH);
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETEXTPART, Win32.MAX_PATH, sbFileExtension);
var fileExtension = sbFileExtension.ToString();
if ( String.IsNullOrEmpty(fileExtension) )
return false;

if (MkdnExtensions.ToLower().Contains(fileExtension.ToLower()))
return true;
Expand All @@ -126,13 +144,31 @@ private bool ValidateHtmlExtension()
StringBuilder sbFileExtension = new StringBuilder(Win32.MAX_PATH);
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETEXTPART, Win32.MAX_PATH, sbFileExtension);
var fileExtension = sbFileExtension.ToString();
if ( String.IsNullOrEmpty(fileExtension) )
return false;

if (HtmlExtensions.ToLower().Contains(fileExtension.ToLower()))
return true;
else
return false;
}

private int ValidateFilterExtension()
{
StringBuilder sbFileExtension = new StringBuilder(Win32.MAX_PATH);
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETEXTPART, Win32.MAX_PATH, sbFileExtension);
var fileExtension = sbFileExtension.ToString();
if ( String.IsNullOrEmpty(fileExtension) )
return -1;

for ( int i = 0; i < filterFound; i++ )
{
if (filterExts[i].Contains(fileExtension.ToLower()))
return i;
}
return -1;
}

protected void UpdateEditorInformation()
{
scintillaGateway.SetScintillaHandle(PluginBase.GetCurrentScintilla());
Expand Down Expand Up @@ -161,7 +197,32 @@ private void OnRenderTimerElapsed(object source, EventArgs e)
else if (ValidateHtmlExtension())
markdownPreviewForm.RenderHtml(GetCurrentEditorText(), notepadPPGateway.GetCurrentFilePath());
else
markdownPreviewForm.RenderMarkdown($"Not a valid Markdown file extension: {MkdnExtensions}\n\nNot a valid HTML file extension: {HtmlExtensions}", notepadPPGateway.GetCurrentFilePath());
{
int filter = ValidateFilterExtension();
if ( filter >= 0 )
{
var filterProgram = filterProgs[filter];
var filterArguments = filterArgs[filter];
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = filterProgram,
Arguments = $"{filterArguments} \"{notepadPPGateway.GetCurrentFilePath()}\"",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};

process.Start();
string data = process.StandardOutput.ReadToEnd();
process.WaitForExit();
markdownPreviewForm.RenderHtml(data, notepadPPGateway.GetCurrentFilePath());
}
else
markdownPreviewForm.RenderMarkdown($"Not a valid Markdown file extension: {MkdnExtensions}\n\nNot a valid HTML file extension: {HtmlExtensions}", notepadPPGateway.GetCurrentFilePath());
}
}
catch
{
Expand Down Expand Up @@ -196,11 +257,25 @@ public void InitCommandMenu()
HtmlExtensions = Win32.ReadIniValue("Options", "HtmlExtensions", iniFilePath, ".html,.htm");
markdownPreviewForm.HtmlFileName = Win32.ReadIniValue("Options", "HtmlFileName", iniFilePath);
markdownPreviewForm.ShowToolbar = Utils.ReadIniBool("Options", "ShowToolbar", iniFilePath);

for ( int i = 0; i < FILTERS; i++ )
{
var section = $"Filter{i}";
filterExts[i] = Win32.ReadIniValue(section, "Extensions", iniFilePath, "!!!");
filterProgs[i] = Win32.ReadIniValue(section, "Program", iniFilePath, "!!!");
filterArgs[i] = Win32.ReadIniValue(section, "Arguments", iniFilePath, "!!!");
if ( filterExts[i].Contains("!!!") )
break;
filterFound++;
}

PluginBase.SetCommand(0, "Toggle &Markdown Panel", TogglePanelVisible);
PluginBase.SetCommand(1, "Synchronize with &caret position", SyncViewWithCaret, syncViewWithCaretPosition);
PluginBase.SetCommand(2, "Synchronize on &vertical scroll", SyncViewWithScroll, syncViewWithScrollPosition);
PluginBase.SetCommand(3, "Edit &Settings", EditSettings);
PluginBase.SetCommand(4, "&About", ShowAboutDialog, new ShortcutKey(false, false, false, Keys.None));
PluginBase.SetCommand(1, "---", null);
PluginBase.SetCommand(2, "Synchronize with &caret position", SyncViewWithCaret, syncViewWithCaretPosition);
PluginBase.SetCommand(3, "Synchronize on &vertical scroll", SyncViewWithScroll, syncViewWithScrollPosition);
PluginBase.SetCommand(4, "---", null);
PluginBase.SetCommand(5, "&Settings", EditSettings);
PluginBase.SetCommand(6, "&About", ShowAboutDialog, new ShortcutKey(false, false, false, Keys.None));

idMyDlg = 0;
}
Expand Down
4 changes: 2 additions & 2 deletions NppMarkdownPanel/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.6.3.*")]
[assembly: AssemblyFileVersion("0.6.3.1")]
[assembly: AssemblyVersion("0.6.4.*")]
[assembly: AssemblyFileVersion("0.6.4.1")]
91 changes: 39 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,34 @@ Plugin to preview Markdown files in Notepad++

### Current Version

My ([VinsWorldcom](https://github.com/VinsWorldcom/NppMarkdownPanel)) changes to [UrsineRaven's repo](https://github.com/UrsineRaven/NppMarkdownPanel) can be found in the [version history](#version-history) below.
The current version with my modifications can be found [here](https://github.com/VinsWorldcom/NppMarkdownPanel/releases)
My ([VinsWorldcom](https://github.com/VinsWorldcom/NppMarkdownPanel)) changes to [UrsineRaven's repo](https://github.com/UrsineRaven/NppMarkdownPanel) can be found [here](https://github.com/VinsWorldcom/NppMarkdownPanel/releases)

The plugin renders Markdown as HTML and provides a viewer. The plugin can also
render HTML documents. Additionally, 10 filters can be created manually in the
configuration file to render documents to HTML for viewing. For example:

```
[Filter0]
Extensions=.pl,.pm
Program=pod2html.bat
Arguments=--css C:\notepad++\plugins\NppMarkdownPanel\style.css
```

will render Perl POD to HTML and display in the viewer panel. There are some
limitations with filters. The rendered views do not synchronize scrolling no
matter what the plugin menu setting is and they do not update "live" with typing,
only update after document save.

<!--
My ([UrsineRaven](https://github.com/UrsineRaven/NppMarkdownPanel)) changes to [mohzy83's repo](https://github.com/mohzy83/NppMarkdownPanel) can be found in the [version history](#version-history) below.
The current version with my modifications (0.5.0.1) can be found [here](https://github.com/UrsineRaven/NppMarkdownPanel/releases)
#### Mohzy83's current version:
The current version is **0.5.0** it can be found [here](https://github.com/mohzy83/NppMarkdownPanel/releases)
-->

### Used libs and icons

Using **Markdig** v 0.16.0 by xoofx - [https://github.com/lunet-io/markdig](https://github.com/lunet-io/markdig)
Expand All @@ -29,12 +48,17 @@ Using portions of nea's **MarkdownViewerPlusPlus** Plugin code - [https://github
Using the **Markdown icon** by dcurtis - [https://github.com/dcurtis/markdown-mark](https://github.com/dcurtis/markdown-mark)

## Prerequisites

- .NET 4.5 or higher

## Installation

### Installation over Notepad++

The plugin can be installed over the integrated Notepad++ "Plugin Admin..".

### Manual Installation

Create the folder "NppMarkdownPanel" in your Notepad++ plugin folder (e.g. "C:\Program Files\Notepad++\plugins") and extract the appropriate zip (x86 or x64) to it.

It should look like this:
Expand Down Expand Up @@ -66,65 +90,28 @@ To open the settings for this plugin: Plugins -> NppMarkdownPanel -> Edit Settin
* #### Show Toolbar in Preview Window
Checking this box will enable the toolbar in the preview window. By default, this is unchecked.

* #### Markdown Extensions
A comma-separated list of file extensions to recognize as Markdown (default = `.md,.mkdn,.mkd`)

* #### HTML Extensions
A comma-separated list of file extensions to recognize as HTML (default = `.html,.htm`)

**Note**: Adding `.xml` to this list will "render" XML files in the viewer if they at least have a valid XML header `<?xml version="1.0"?>`.

### Preview Window Toolbar

* #### Save As... (![save-btn](help/save-btn.png "Picture of the Save button on the preview panel toolbar"))
Clicking this button allows you to save the rendered preview as an HTML document.

### Synchronize viewer with caret position
### Synchronize with caret position

Enabling this in the plugin's menu (Plugins -> NppMarkdownPanel) makes the preview panel stay in sync with the caret in the markdown document that is being edited.
This is similar to the _Synchronize Vertical Scrolling_ option of Notepad++ for keeping two open editing panels scrolling together.


## Version History

Refer to Git commit history for latest changes.

### VinsWorldcom (Version 0.6.2.1)
- now previews / parses HTML files natively
- fix statusbar overlay issue in preview panel

### VinsWorldcom (Version 0.6.1.1)
- rename plugin menu to "Markdown Panel"
- add status bar to panel for mouse-over URL status
- add file extensions to settings dialogue
- sync view with vertical scroll option
- bugfix: now parses when document is in Scintilla second view
- bugfix: automatically update view when typing in N++

### UrsineRaven change set 1 (Version 0.5.0.1)
- add ability to save the preview to HTML document
- can automatically save every time it's rendered
- can show a toolbar on the preview panel to allow saving on button click
- add settings to settings window to allow:
- selecting the file for automatic saving
- enabling/disabling the preview panel toolbar
- added some basic validation for some of the fields
- updated README.md
- added a section describing the Settings options
- added my changes to version history

### Version 0.5.0
- change zoomlevel for the preview in settings dialog
- change css file for the markdown style
- the new settings are persistent
- open settings dialog: Plugins-> NppMarkdownPanel -> Edit Settings
![npp-settings](help/open-settings.png "open settings dialog")

### Version 0.4.0
- switched from CommonMark.Net to markdig rendering library

### Version 0.3.0
- synchronize viewer with caret position

### Version 0.2.0
- Initial release
Enabling this in the plugin's menu (Plugins -> Markdown Panel) makes the preview panel stay in sync with the caret in the markdown document that is being edited. This is similar to the _Synchronize Vertical Scrolling_ option of Notepad++ for keeping two open editing panels scrolling together.

![npp-sync-caret](help/sync_caret.gif "Synchronize viewer with caret position")

### Feature - Synchronize viewer with caret position
### Synchronize on vertical scroll

![npp-sync-caret](help/sync_caret.gif "Synchronize viewer with caret position")
Enabling this in the plugin's menu (Plugins -> Markdown Panel) attempts to do a better job at synchronizing scrolling between the preview panel and the document that is being edited without the need for caret movement (in other words, just using scrollbars should sync too).


## License
Expand Down

0 comments on commit 1325fcc

Please sign in to comment.