Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 86 additions & 17 deletions pp-source-format/Formatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json.Nodes;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
Expand Down Expand Up @@ -48,7 +48,6 @@ static PpPasteDataType PowerpointPasteType(this Format f)
return PpPasteDataType.ppPasteRTF;
default:
throw new Exception("Unknown paste type: " + f.ToString());

}
}

Expand All @@ -65,7 +64,6 @@ static string DataFormat(this Format f)
return DataFormats.Rtf;
default:
throw new Exception("Unknown data format: " + f.ToString());

}
}

Expand All @@ -82,10 +80,77 @@ static string PygmentsFormat(this Format f)
return "rtf";
default:
throw new Exception("Unknown pygments format: " + f.ToString());
}
}

public class PygmentsConfiguration
{
public class Style
{
public string Name { get; }
public string Info { get; }

public Style(string name, string info)
{
Name = name;
Info = info;
}
}

public IEnumerable<string> Lexers { get; }
public List<Style> Styles { get; }

public PygmentsConfiguration(List<string> lexers, List<Style> styles)
{
Lexers = lexers;
Styles = styles;
}
}

public static PygmentsConfiguration GetConfiguration()
{
var allArguments = new List<string>(new string[]
{
"-L",
"--json",
});


var arguments = String.Join(" ", allArguments.ToArray());
var startInfo = new ProcessStartInfo()
{
FileName = Pygments.PygmentizePath,
Arguments = arguments,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true,
CreateNoWindow = true,
};

Process p = new Process()
{
StartInfo = startInfo
};

p.Start();
var result = Task.Run(() => p.StandardOutput.ReadToEnd()).Result;
var error = Task.Run(() => p.StandardError.ReadToEnd()).Result;
p.WaitForExit();

if (!String.IsNullOrWhiteSpace(error))
{
throw new Exception(
String.Format("Error getting supported Pygmentzie lexers and styules {0}:\n", error));
}

var config = JsonNode.Parse(result);
var lexerObject = config["lexers"].AsObject();
var stylesObject = config["styles"].AsObject();
var lexers = lexerObject.Select(x => x.Key).ToList();
var styles = stylesObject.Select(x => new PygmentsConfiguration.Style(x.Key, x.Value.AsObject()["doc"].GetValue<string>())).ToList();
return new PygmentsConfiguration(lexers, styles);
}

/// <summary>
/// Exactly what it says on the tin.
/// </summary>
Expand All @@ -100,7 +165,6 @@ public static void FormatShape(Shape s, string language, string style)

try
{

// Every paste option I tried had different issues, so I use this
// switch to switch between them.
Format format = Format.HTML;
Expand All @@ -125,6 +189,7 @@ public static void FormatShape(Shape s, string language, string style)
var formattedText = RunPygments(item, sourceText, language, style);
d.SetData(item.DataFormat(), formattedText);
}

Clipboard.SetDataObject(d);
}
else
Expand Down Expand Up @@ -200,12 +265,14 @@ private static string RunPygments(Format format, string input, string language,
var filePath = Path.Combine(Path.GetTempPath(), "pp-format." + format.PygmentsFormat());
bool useFilePath = false;

var options = new List<string>(new string[] {
var options = new List<string>(new string[]
{
"style=" + style,
});
if (format == Format.HTML)
{
options.AddRange(new string[] {
options.AddRange(new string[]
{
"noclasses=true",
"nowrap=true",
//"full=true",
Expand All @@ -214,7 +281,8 @@ private static string RunPygments(Format format, string input, string language,
}


var allArguments = new List<string>(new string[] {
var allArguments = new List<string>(new string[]
{
"-f " + format.PygmentsFormat(),
"-l " + language,
"-O " + '"' + string.Join(",", options.ToArray()) + '"'
Expand Down Expand Up @@ -249,25 +317,27 @@ private static string RunPygments(Format format, string input, string language,
};

p.Start();

p.StandardInput.Write(input);
p.StandardInput.Close();
var result = Task.Run(() => useFilePath ? File.ReadAllText(filePath) : p.StandardOutput.ReadToEnd()).Result;
var error = Task.Run(() => p.StandardError.ReadToEnd()).Result;

p.WaitForExit();


var error = p.StandardError.ReadToEnd();
if (!String.IsNullOrWhiteSpace(error))
{
throw new Exception(String.Format("Temp file at {0}, Error running pygmentize with arguments {0}:\n{1}", filePath, arguments, error));
throw new Exception(String.Format("Temp file at {0}, Error running pygmentize with arguments {0}:\n{1}",
filePath, arguments, error));
}

var result = useFilePath ? File.ReadAllText(filePath) : p.StandardOutput.ReadToEnd();

if (format == Format.HTML)
{
// Wrap the result in a valid, minimal document with the Fragments as required by the HTML Clipboard format
// https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa767917(v=vs.85)
result = "<html><body>" + ClipboardHelper.StartFragment + "<pre>" + result + "</pre>" + ClipboardHelper.EndFragment + "</body></html>";
result = "<html><body>" + ClipboardHelper.StartFragment + "<pre>" + result + "</pre>" +
ClipboardHelper.EndFragment + "</body></html>";

// Whitespace at the beginning of lines seems to be collapsed, sadly neither of the
// tricks at https://stackoverflow.com/questions/47475774/how-to-add-spaces-to-html-clipboard-data-so-that-winword-inserts-them-on-pasting
Expand All @@ -279,10 +349,8 @@ private static string RunPygments(Format format, string input, string language,

// Okay, this is the most nasty part ... We insert &nbsp; after each linebreak to preserve
// whitespace formatting. Each whitespace must be replaced by a single non breaking space.
result = Regex.Replace(result, "(<br> *)", delegate (Match m)
{
return m.Value.Replace(" ", "&nbsp;");
});
result = Regex.Replace(result, "(<br> *)",
delegate(Match m) { return m.Value.Replace(" ", "&nbsp;"); });

if (useFilePath)
{
Expand Down Expand Up @@ -325,10 +393,11 @@ private static string HtmlToRtf(string toPaste)
{
System.Windows.Forms.Application.DoEvents();
}

web.Document.ExecCommand("SelectAll", false, null);
web.Document.ExecCommand("Copy", false, null);
//web.Dispose();
return Clipboard.GetData(DataFormats.Rtf) as string;
}
}
}
}
2 changes: 1 addition & 1 deletion pp-source-format/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 0 additions & 14 deletions pp-source-format/Ribbon.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions pp-source-format/Ribbon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,37 @@ private void RibbonLoad(object sender, RibbonUIEventArgs e)
/// </summary>
private void ReflectPygmentizeStatus()
{
var configuration = Formatter.GetConfiguration();

if (Pygments.FoundPygmentize)
{
lblPygmentsAvailable.SuperTip = Pygments.PygmentizePath;

SetBoxVisible(bxAvailable, true);
SetBoxVisible(bxUnavailable, false);

cmbLanguage.Items.Clear();
cmbStyle.Items.Clear();

foreach (var lexer in configuration.Lexers)
{
var item = Factory.CreateRibbonDropDownItem();
item.Label = lexer;
cmbLanguage.Items.Add(item);
}

cmbLanguage.Text = CurrentSettings.SelectedLanguage;

foreach (var style in configuration.Styles)
{
var item = Factory.CreateRibbonDropDownItem();
item.Label = style.Name;
item.ScreenTip = style.Info;
cmbStyle.Items.Add(item);
}

cmbStyle.Text = CurrentSettings.SelectedStyle;

}
else
{
Expand Down
12 changes: 10 additions & 2 deletions pp-source-format/app.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="pp_source_format.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
Expand All @@ -15,4 +15,12 @@
</setting>
</pp_source_format.Properties.Settings>
</userSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
12 changes: 12 additions & 0 deletions pp-source-format/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Bcl.AsyncInterfaces" version="8.0.0" targetFramework="net472" />
<package id="System.Buffers" version="4.5.1" targetFramework="net472" />
<package id="System.Memory" version="4.5.5" targetFramework="net472" />
<package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net472" />
<package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net472" />
<package id="System.Text.Encodings.Web" version="8.0.0" targetFramework="net472" />
<package id="System.Text.Json" version="8.0.3" targetFramework="net472" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net472" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net472" />
</packages>
32 changes: 31 additions & 1 deletion pp-source-format/pp-source-format.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<PublishUrl>publish\</PublishUrl>
<InstallUrl />
<TargetCulture>en</TargetCulture>
<ApplicationVersion>0.1.0.4</ApplicationVersion>
<ApplicationVersion>0.1.0.5</ApplicationVersion>
<AutoIncrementApplicationRevision>true</AutoIncrementApplicationRevision>
<UpdateEnabled>false</UpdateEnabled>
<UpdateInterval>0</UpdateInterval>
Expand Down Expand Up @@ -123,9 +123,38 @@
-->
<ItemGroup>
<Reference Include="Accessibility" />
<Reference Include="Microsoft.Bcl.AsyncInterfaces, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Bcl.AsyncInterfaces.8.0.0\lib\net462\Microsoft.Bcl.AsyncInterfaces.dll</HintPath>
</Reference>
<Reference Include="mscorlib" />
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Memory, Version=4.0.1.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Memory.4.5.5\lib\net461\System.Memory.dll</HintPath>
</Reference>
<Reference Include="System.Numerics" />
<Reference Include="System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.6.0.0\lib\net461\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
</Reference>
<Reference Include="System.Text.Encodings.Web, Version=8.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Encodings.Web.8.0.0\lib\net462\System.Text.Encodings.Web.dll</HintPath>
</Reference>
<Reference Include="System.Text.Json, Version=8.0.0.3, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Text.Json.8.0.3\lib\net462\System.Text.Json.dll</HintPath>
</Reference>
<Reference Include="System.Threading.Tasks.Extensions, Version=4.2.0.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Threading.Tasks.Extensions.4.5.4\lib\net461\System.Threading.Tasks.Extensions.dll</HintPath>
</Reference>
<Reference Include="System.ValueTuple, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.ValueTuple.4.5.0\lib\net47\System.ValueTuple.dll</HintPath>
</Reference>
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />
Expand Down Expand Up @@ -195,6 +224,7 @@
<DependentUpon>Ribbon.cs</DependentUpon>
</EmbeddedResource>
<None Include="app.config" />
<None Include="packages.config" />
<None Include="pp-source-format_TemporaryKey.pfx" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
Expand Down