Skip to content

Commit

Permalink
Merge pull request #147 from MaceWindu/feature/nrt
Browse files Browse the repository at this point in the history
Add NRT annotations
  • Loading branch information
Sicos1977 authored Mar 12, 2024
2 parents 32bf380 + 886d065 commit 6bf209f
Show file tree
Hide file tree
Showing 55 changed files with 597 additions and 623 deletions.
1 change: 1 addition & 0 deletions ChromiumHtmlToPdfConsole/ChromiumHtmlToPdfConsole.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<RootNamespace>ChromiumHtmlToPdfConsole</RootNamespace>
<FileVersion>4.2.1.0</FileVersion>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Folder Include="Properties\" />
Expand Down
4 changes: 2 additions & 2 deletions ChromiumHtmlToPdfConsole/ConversionItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal class ConversionItem
/// <summary>
/// The exception when <see cref="Status"/> is <see cref="ConversionItemStatus.Failed"/>
/// </summary>
public Exception Exception { get; private set; }
public Exception? Exception { get; private set; }

/// <summary>
/// Returns this object as a comma separated string
Expand All @@ -73,7 +73,7 @@ internal ConversionItem(ConvertUri inputUri, string outputFile)
/// </summary>
/// <param name="status"><see cref="ConversionItemStatus"/></param>
/// <param name="exception"></param>
public void SetStatus(ConversionItemStatus status, Exception exception = null)
public void SetStatus(ConversionItemStatus status, Exception? exception = null)
{
Status = status;
Exception = exception;
Expand Down
2 changes: 1 addition & 1 deletion ChromiumHtmlToPdfConsole/LimitedConcurrencyLevel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private void NotifyThreadPoolOfPendingWork()
// Get the next item from the queue
// ReSharper disable once PossibleNullReferenceException
item = _tasks.First.Value;
item = _tasks.First!.Value;
_tasks.RemoveFirst();
}
Expand Down
46 changes: 23 additions & 23 deletions ChromiumHtmlToPdfConsole/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class Options
/// The input url or file
/// </summary>
[Option("input", Required = true, HelpText = "The input content, url or file")]
public string Input { get; set; }
public string Input { get; set; } = null!;

/// <summary>
/// A file with input urls and/or files
Expand All @@ -34,7 +34,7 @@ public class Options
/// The output file
/// </summary>
[Option("output", Required = true, HelpText = "The output file")]
public string Output { get; set; }
public string Output { get; set; } = null!;

/// <summary>
/// Paper orientation. Defaults to false.
Expand Down Expand Up @@ -111,7 +111,7 @@ public class Options
/// </summary>
[Option("user-agent", Required = false,
HelpText = "Let Chromium know that we want to use the given user-agent string instead of the default one")]
public string UserAgent { get; set; }
public string? UserAgent { get; set; }

/// <summary>
/// Top margin in inches. Defaults to 1cm (~0.4 inches).
Expand Down Expand Up @@ -141,7 +141,7 @@ public class Options
/// Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages.
/// </summary>
[Option("pageranges", Required = false, HelpText = "Paper ranges to print, e.g., '1-5, 8, 11-13'")]
public string PageRanges { get; set; }
public string? PageRanges { get; set; }

/// <summary>
/// Whether to silently ignore invalid but successfully parsed page ranges, such as '3-2'. Defaults to false.
Expand All @@ -158,21 +158,21 @@ public class Options
"The location for Chrome or Edge, when not set then this tool first looks inside the folder where " +
"it is executed from if it can find Chromium (portable) otherwise the registry is accessed " +
"to get the needed information")]
public string ChromiumLocation { get; set; }
public string? ChromiumLocation { get; set; }

/// <summary>
/// The location for Chrome or Edge, when not set then the registry is accessed to get the needed information
/// </summary>
[Option("chromium-userprofile", Required = false,
HelpText =
"The location where Chromium can store it's user profile")]
public string ChromiumUserProfile { get; set; }
public string? ChromiumUserProfile { get; set; }

/// <summary>
/// This tells Chromium to use a custom proxy configuration
/// </summary>
[Option("proxy-server", Required = false, HelpText = "This tells Chromium to use a custom proxy configuration")]
public string ProxyServer { get; set; }
public string? ProxyServer { get; set; }

/// <summary>
/// This tells Chromium to bypass any specified proxy for the given semi-colon-separated list of hosts. This flag must be
Expand All @@ -185,34 +185,34 @@ public class Options
"This flag must be used (or rather, only has an effect) in tandem with --proxy-server. " +
"For example \"*.google.com;*foo.com;127.0.0.1:8080\""
)]
public string ProxyByPassList { get; set; }
public string? ProxyByPassList { get; set; }

/// <summary>
/// This tells Chromium to use the PAC file at the specified URL. For example "http://wpad/windows.pac"
/// </summary>
[Option("proxy-pac-url", Required = false,
HelpText =
"This tells Chromium to use the PAC file at the specified URL. For example \"http://wpad/windows.pac\"")]
public string ProxyPacUrl { get; set; }
public string? ProxyPacUrl { get; set; }

/// <summary>
/// Run Chrome or Edge under this user. This option is used in combination with --password"
/// </summary>
[Option("user", Required = false,
HelpText = "Run Chrome or Edge under this user. This option is used in combination with --password")]
public string User { get; set; }
public string? User { get; set; }

/// <summary>
/// The password needed for --user
/// </summary>
[Option("password", Required = false, HelpText = "The password needed for --user")]
public string Password { get; set; }
public string? Password { get; set; }

/// <summary>
/// The password needed for --user
/// </summary>
[Option("tempfolder", Required = false, HelpText = "A folder where this tool can put temporary files")]
public string TempFolder { get; set; }
public string? TempFolder { get; set; }

/// <summary>
/// Use multi threading when converting. Only useful if the parameter --input-is-list is used
Expand All @@ -235,9 +235,9 @@ public class Options
/// <summary>
/// Wait for the javascript window.status to equal the given string before starting PDF conversion
/// </summary>
[Option("wait-for-window-status", Required = false, Default = "",
[Option("wait-for-window-status", Required = false,
HelpText = "Wait for the javascript window.status to equal the given string before starting PDF conversion")]
public string WaitForWindowStatus { get; set; }
public string? WaitForWindowStatus { get; set; }

/// <summary>
/// The timeout when waiting for the parameter <see cref="WaitForWindowStatus"/>
Expand Down Expand Up @@ -267,14 +267,14 @@ public class Options
/// </summary>
[Option("pre-wrap-file-extensions", Required = false,
HelpText = "The files to wrap in a HTML file with a <PRE> tag")]
public IEnumerable<string> PreWrapFileExtensions { get; set; }
public IEnumerable<string>? PreWrapFileExtensions { get; set; }

/// <summary>
/// The encoding that is used for the <see cref="Input"/> file
/// </summary>
[Option("encoding", Required = false, Default = "",
[Option("encoding", Required = false,
HelpText = "The encoding that is used for the --input file")]
public string Encoding { get; set; }
public string? Encoding { get; set; }

/// <summary>
/// Resize images so that they fit the width of the page
Expand Down Expand Up @@ -311,17 +311,17 @@ public class Options
/// <summary>
/// When set then the logging gets written to this file instead of the console
/// </summary>
[Option("logfile", Required = false, Default = "",
[Option("logfile", Required = false,
HelpText = "When set then the logging gets written to this file instead of the console " +
"(Wildcards {PID}, {DATE}, {TIME})")]
public string LogFile { get; set; }
public string? LogFile { get; set; }

/// <summary>
/// Runs the given javascript after the webpage has been loaded
/// </summary>
[Option("run-javascript", Required = false, Default = "",
[Option("run-javascript", Required = false,
HelpText = "Runs the given javascript after the webpage has been loaded and before it is converted to PDF")]
public string RunJavascript { get; set; }
public string? RunJavascript { get; set; }

/// <summary>
/// This tells chrome to bypass any specified proxy for the given semi-colon-separated list of hosts. This flag must be
Expand All @@ -333,7 +333,7 @@ public class Options
"This tells Chromium to blacklist certain urls by blocking them. " +
"For example \"*.google.com;*foo.com;\""
)]
public string UrlBlacklist { get; set; }
public string? UrlBlacklist { get; set; }

/// <summary>
/// This will capture a snapshot of the webpage (before it is converted to PDF) and save this to disk with
Expand Down Expand Up @@ -364,7 +364,7 @@ public class Options
/// When set then Chrome or Edge uses this directory for caching
/// </summary>
[Option("disk-cache-directory", Required = false, HelpText = "When set then Chrome or Edge uses this directory for caching")]
public string DiskCacheDirectory { get; set; }
public string? DiskCacheDirectory { get; set; }

/// <summary>
/// When set then Chrome or Edge uses this directory for caching
Expand Down
54 changes: 25 additions & 29 deletions ChromiumHtmlToPdfConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
Expand All @@ -23,33 +24,23 @@ static class Program
/// <summary>
/// When set then logging is written to this stream
/// </summary>
private static Stream _logger;
private static Stream? _logger;

/// <summary>
/// <see cref="LimitedConcurrencyLevel" />
/// </summary>
private static TaskFactory _taskFactory;

/// <summary>
/// A list with <see cref="ConversionItem"/>'s to process
/// </summary>
private static ConcurrentQueue<ConversionItem> _itemsToConvert;

/// <summary>
/// A list with converted <see cref="ConversionItem"/>'s
/// </summary>
private static ConcurrentQueue<ConversionItem> _itemsConverted;
private static TaskFactory? _taskFactory;

/// <summary>
/// Used to keep track of all the worker tasks we are starting
/// </summary>
private static List<Task> _workerTasks;
private static List<Task>? _workerTasks;
#endregion

#region Main
public static void Main(string[] args)
{
Options options = null;
Options? options = null;

try
{
Expand All @@ -75,8 +66,8 @@ public static void Main(string[] args)

if (options.InputIsList)
{
_itemsToConvert = new ConcurrentQueue<ConversionItem>();
_itemsConverted = new ConcurrentQueue<ConversionItem>();
var itemsToConvert = new ConcurrentQueue<ConversionItem>();
var itemsConverted = new ConcurrentQueue<ConversionItem>();

WriteToLog($"Reading input file '{options.Input}'");
var lines = File.ReadAllLines(options.Input);
Expand All @@ -101,12 +92,12 @@ public static void Main(string[] args)

outputFile = Path.ChangeExtension(outputFile, ".pdf");

_itemsToConvert.Enqueue(new ConversionItem(inputUri,
itemsToConvert.Enqueue(new ConversionItem(inputUri,
// ReSharper disable once AssignNullToNotNullAttribute
Path.Combine(outputPath, outputFile)));
}

WriteToLog($"{_itemsToConvert.Count} items read");
WriteToLog($"{itemsToConvert.Count} items read");

if (options.UseMultiThreading)
{
Expand All @@ -117,19 +108,19 @@ public static void Main(string[] args)
{
var i1 = i;
_workerTasks.Add(_taskFactory.StartNew(() =>
ConvertWithTask(options, (i1 + 1).ToString())));
ConvertWithTask(itemsToConvert, itemsConverted, options, (i1 + 1).ToString())));
}

WriteToLog("Started");

Task.WaitAll(_workerTasks.ToArray());
}
else
ConvertWithTask(options, null).GetAwaiter().GetResult();
ConvertWithTask(itemsToConvert, itemsConverted, options, null).GetAwaiter().GetResult();

// Write conversion information to output file
using var output = File.OpenWrite(options.Output);
foreach (var itemConverted in _itemsConverted)
foreach (var itemConverted in itemsConverted)
{
var bytes = new UTF8Encoding(true).GetBytes(itemConverted.OutputLine);
output.Write(bytes, 0, bytes.Length);
Expand Down Expand Up @@ -157,9 +148,9 @@ public static void Main(string[] args)
/// </summary>
/// <param name="args"></param>
/// <param name="options"><see cref="Options"/></param>
private static void ParseCommandlineParameters(IEnumerable<string> args, out Options options)
private static void ParseCommandlineParameters(IEnumerable<string> args, out Options? options)
{
Options tempOptions = null;
Options? tempOptions = null;

var errors = false;
var parser = new Parser(settings =>
Expand Down Expand Up @@ -246,6 +237,7 @@ private static PageSettings GetPageSettings(Options options)
/// </summary>
/// <param name="options"><see cref="Options"/></param>
/// <returns>Maximum concurrency level</returns>
[MemberNotNull(nameof(_taskFactory))]
private static int SetMaxConcurrencyLevel(Options options)
{
var maxConcurrencyLevel = Environment.ProcessorCount;
Expand Down Expand Up @@ -286,7 +278,11 @@ private static void SetConverterSettings(Converter converter, Options options)
if (!string.IsNullOrWhiteSpace(options.ProxyServer))
{
converter.SetProxyServer(options.ProxyServer);
converter.SetProxyBypassList(options.ProxyByPassList);

if (!string.IsNullOrWhiteSpace(options.ProxyByPassList))
{
converter.SetProxyBypassList(options.ProxyByPassList);
}
}

if (!string.IsNullOrWhiteSpace(options.ProxyPacUrl))
Expand Down Expand Up @@ -398,11 +394,11 @@ private static ConvertUri CheckInput(Options options)
#region ConvertWithTask
/// <summary>
/// This function is started from a <see cref="Task"/> and processes <see cref="ConversionItem"/>'s
/// that are in the <see cref="_itemsToConvert"/> queue
/// that are in the <paramref name="itemsToConvert"/> queue
/// </summary>
/// <param name="options"></param>
/// <param name="instanceId"></param>
private static async Task ConvertWithTask(Options options, string instanceId)
private static async Task ConvertWithTask(ConcurrentQueue<ConversionItem> itemsToConvert, ConcurrentQueue<ConversionItem> itemsConverted, Options options, string? instanceId)
{
var pageSettings = GetPageSettings(options);

Expand All @@ -419,9 +415,9 @@ private static async Task ConvertWithTask(Options options, string instanceId)

SetConverterSettings(converter, options);

while (!_itemsToConvert.IsEmpty)
while (!itemsToConvert.IsEmpty)
{
if (!_itemsToConvert.TryDequeue(out var itemToConvert)) continue;
if (!itemsToConvert.TryDequeue(out var itemToConvert)) continue;
try
{
await converter.ConvertToPdfAsync(itemToConvert.InputUri, itemToConvert.OutputFile, pageSettings,
Expand All @@ -435,7 +431,7 @@ await converter.ConvertToPdfAsync(itemToConvert.InputUri, itemToConvert.OutputFi
itemToConvert.SetStatus(ConversionItemStatus.Failed, exception);
}

_itemsConverted.Enqueue(itemToConvert);
itemsConverted.Enqueue(itemToConvert);
}
}
}
Expand Down
Loading

0 comments on commit 6bf209f

Please sign in to comment.