Skip to content
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

Fix for duplicate key bug #36

Closed
wants to merge 7 commits into from
Closed
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
4 changes: 2 additions & 2 deletions Snap2HTML/CommandLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ public Arguments(IEnumerable<string> arguments)
//Because of the split index 0 will be a empty string
string valuesWithoutQuotes = RemoveMatchingQuotes(parts[2]);

// MOD: Don't split on commas
// MOD: Don't split on commas
//AddListValues(parts[1], valuesWithoutQuotes.Split(','));
Add( parts[1], valuesWithoutQuotes );
Add( parts[1], valuesWithoutQuotes );
break;
}
}
Expand Down
10 changes: 5 additions & 5 deletions Snap2HTML/Settings.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
namespace Snap2HTML.Properties {
// This class allows you to handle specific events on the settings class:
// The SettingChanging event is raised before a setting's value is changed.
// The PropertyChanged event is raised after a setting's value is changed.
// The SettingsLoaded event is raised after the setting values are loaded.
// The SettingsSaving event is raised before the setting values are saved.
// This class allows you to handle specific events on the settings class:
// The SettingChanging event is raised before a setting's value is changed.
// The PropertyChanged event is raised after a setting's value is changed.
// The SettingsLoaded event is raised after the setting values are loaded.
// The SettingsSaving event is raised before the setting values are saved.
internal sealed partial class Settings
{
public Settings()
Expand Down
5 changes: 4 additions & 1 deletion Snap2HTML/app.config
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@
</setting>
</Snap2HTML.Properties.Settings>
</userSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
72 changes: 40 additions & 32 deletions Snap2HTML/frmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

namespace Snap2HTML
{
public partial class frmMain : Form
{
public partial class frmMain : Form
{
private bool initDone = false;
private bool runningAutomated = false;

public frmMain()
{
InitializeComponent();
}
public frmMain()
{
InitializeComponent();
}

private void frmMain_Load( object sender, EventArgs e )
{
Expand Down Expand Up @@ -58,14 +58,14 @@ private void frmMain_Load( object sender, EventArgs e )
}

private void frmMain_Shown( object sender, EventArgs e )
{
// parse command line
var commandLine = Environment.CommandLine;
{
// parse command line
var commandLine = Environment.CommandLine;
commandLine = commandLine.Replace( "-output:", "-outfile:" ); // correct wrong parameter to avoid confusion
var splitCommandLine = Arguments.SplitCommandLine(commandLine);
var arguments = new Arguments(splitCommandLine);
var splitCommandLine = Arguments.SplitCommandLine(commandLine);
var arguments = new Arguments(splitCommandLine);

// first test for single argument (ie path only)
// first test for single argument (ie path only)
if( splitCommandLine.Length == 2 && !arguments.Exists( "path" ) )
{
if( System.IO.Directory.Exists( splitCommandLine[1] ) )
Expand All @@ -76,7 +76,7 @@ private void frmMain_Shown( object sender, EventArgs e )

var settings = new SnapSettings();
if( arguments.Exists( "path" ) && arguments.Exists( "outfile" ) )
{
{
this.runningAutomated = true;

settings.rootFolder = arguments.Single( "path" );
Expand Down Expand Up @@ -119,7 +119,7 @@ private void frmMain_Shown( object sender, EventArgs e )
settings.title = arguments.Single( "title" );
}

}
}

// keep window hidden in silent mode
if( arguments.IsTrue( "silent" ) && this.runningAutomated )
Expand All @@ -135,7 +135,7 @@ private void frmMain_Shown( object sender, EventArgs e )
{
StartProcessing( settings );
}
}
}

private void frmMain_FormClosing( object sender, FormClosingEventArgs e )
{
Expand All @@ -150,10 +150,18 @@ private void frmMain_FormClosing( object sender, FormClosingEventArgs e )
}

private void cmdBrowse_Click(object sender, EventArgs e)
{
folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop; // this makes it possible to select network paths too
{
folderBrowserDialog1.RootFolder = Environment.SpecialFolder.Desktop; // this makes it possible to select network paths too

// Use the following registry key when some network shares do not show up in the dialog
// and reboot the system to apply the change.
//Windows Registry Editor Version 5.00

//[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System]
// "EnableLinkedConnections" = dword:00000001

folderBrowserDialog1.SelectedPath = txtRoot.Text;
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
try
{
Expand All @@ -164,10 +172,10 @@ private void cmdBrowse_Click(object sender, EventArgs e)
MessageBox.Show( "Could not select folder:\n\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
SetRootPath( "", false );
}
}
}
}
}

private void cmdCreate_Click(object sender, EventArgs e)
private void cmdCreate_Click(object sender, EventArgs e)
{
// ask for output file
string fileName = new System.IO.DirectoryInfo( txtRoot.Text + @"\" ).Name;
Expand All @@ -177,13 +185,13 @@ private void cmdCreate_Click(object sender, EventArgs e)
fileName = fileName.Replace(invalid[i].ToString(), "");
}

saveFileDialog1.DefaultExt = "html";
saveFileDialog1.DefaultExt = "html";
if( !fileName.ToLower().EndsWith( ".html" ) ) fileName += ".html";
saveFileDialog1.FileName = fileName;
saveFileDialog1.Filter = "HTML files (*.html)|*.html|All files (*.*)|*.*";
saveFileDialog1.InitialDirectory = System.IO.Path.GetDirectoryName(txtRoot.Text);
saveFileDialog1.InitialDirectory = System.IO.Path.GetDirectoryName(txtRoot.Text);
saveFileDialog1.CheckPathExists = true;
if (saveFileDialog1.ShowDialog() != DialogResult.OK) return;
if (saveFileDialog1.ShowDialog() != DialogResult.OK) return;

if( !saveFileDialog1.FileName.ToLower().EndsWith( ".html" ) ) saveFileDialog1.FileName += ".html";

Expand Down Expand Up @@ -256,13 +264,13 @@ private void backgroundWorker_RunWorkerCompleted( object sender, RunWorkerComple
}
}

private void chkLinkFiles_CheckedChanged(object sender, EventArgs e)
{
if (chkLinkFiles.Checked == true)
txtLinkRoot.Enabled = true;
else
txtLinkRoot.Enabled = false;
}
private void chkLinkFiles_CheckedChanged(object sender, EventArgs e)
{
if (chkLinkFiles.Checked == true)
txtLinkRoot.Enabled = true;
else
txtLinkRoot.Enabled = false;
}

// Link Label handlers
private void linkLabel1_LinkClicked( object sender, LinkLabelLinkClickedEventArgs e )
Expand Down Expand Up @@ -360,5 +368,5 @@ private void SetRootPath( string path, bool pathIsValid = true )
}
}

}
}
}
58 changes: 21 additions & 37 deletions Snap2HTML/frmMain_BackgroundWorker.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CommandLine.Utility;
using System.IO;
using System.Diagnostics;
using System.Linq;

namespace Snap2HTML
{
Expand All @@ -33,7 +31,7 @@ private void backgroundWorker_DoWork( object sender, DoWorkEventArgs e )

// Calculate some stats
int totDirs = 0;
int totFiles = 0;
long totFiles = 0;
long totSize = 0;
foreach( var folder in content )
{
Expand Down Expand Up @@ -181,11 +179,7 @@ private static List<SnappedFolder> GetContent( SnapSettings settings, Background
{
// Get folder properties
var dirName = dirs[d];
var currentDir = new SnappedFolder( Path.GetFileName( dirName ), Path.GetDirectoryName( dirName ) );
if( dirName == Path.GetPathRoot( dirName ) )
{
currentDir = new SnappedFolder( "", dirName );
}
var currentDir = new SnappedFolder( Path.GetFileName( dirName ), Path.GetDirectoryName( dirName ) ?? dirName );

modified_date = "";
created_date = "";
Expand Down Expand Up @@ -349,38 +343,28 @@ private static void BuildJavascriptContentArray( List<SnappedFolder> content, in

var lineBreakSymbol = ""; // Could be set to \n to make the html output more readable, at the expense of increased size

// Assign an ID to each folder. This is equal to the index in the JS data array
var dirIndexes = new Dictionary<string, string>();
for( var i = 0; i < content.Count; i++ )
{
dirIndexes.Add( content[i].GetFullPath(), ( i + startIndex ).ToString() );
}

// Build a lookup table with subfolder IDs for each folder
var subdirs = new Dictionary<string, List<string>>();
foreach( var dir in content )
{
// add all folders as keys
subdirs.Add( dir.GetFullPath(), new List<string>() );
}
if( !subdirs.ContainsKey( content[0].Path ) && content[0].Name != "" )
{
// ensure that root folder is not missed missed
subdirs.Add( content[0].Path, new List<string>() );
}
foreach( var dir in content )

foreach( var dirInfo in content.Select((value, index) => new { value.Name, value.Path, FullPath = value.GetFullPath(), Index = index + startIndex } ))
{
if( dir.Name != "" )
// for each folder, add its index to its parent folder list of subdirs
// The ID for each folder is equal to the index in the JS data array
if( subdirs.TryGetValue( dirInfo.Path, out var subfolderIds ) )
{
try
{
// for each folder, add its index to its parent folder list of subdirs
subdirs[dir.Path].Add( dirIndexes[dir.GetFullPath()] );
}
catch( Exception ex )
{
// orphan file or folder?
}
subfolderIds.Add( dirInfo.Index.ToString() );
}
else
{
// the root may not reference itself in the JS output
bool isDiskRoot = dirInfo.Name == string.Empty;
subdirs.Add( dirInfo.Path, isDiskRoot ? new List<string>() : new List<string>() { dirInfo.Index.ToString() } );
}

// all folders must exist as key, also those without children
if( !subdirs.ContainsKey( dirInfo.FullPath ) )
{
subdirs.Add( dirInfo.FullPath, new List<string>() );
}
}

Expand Down