📄 exportprojecttohtmldialog.cs
字号:
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Markus Palme" email="MarkusPalme@gmx.de"/>
// <version>$Revision: 915 $</version>
// </file>
using System;
using System.Drawing;
using System.Collections;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using ICSharpCode.TextEditor.Document;
using ICSharpCode.Core;
using ICSharpCode.TextEditor.Util;
using ICSharpCode.SharpDevelop.Project;
namespace ICSharpCode.SharpDevelop.Gui
{
public class ExportProjectToHtmlDialog : Form
{
TextBox pathTextBox = new TextBox();
Label pathLabel = new Label();
Button okButton = new Button();
Button cancelButton = new Button();
Button browseButton = new Button();
ProgressBar progressBar = new ProgressBar();
IProject project;
int filesExported = 0;
delegate void ProgressBarDelegate();
int FilesExported
{
get {
return filesExported;
}
set
{
filesExported = value;
if(this.InvokeRequired)
{
this.Invoke(new ProgressBarDelegate(setProgressBar), new object[] {});
}
else
{
progressBar.Value = filesExported = value;
}
}
}
void setProgressBar()
{
FilesExported = FilesExported;
}
void StartExporting()
{
Controls.Clear();
progressBar.Location = new Point(8, 8);
progressBar.Size = new Size(Width - 16, 16);
progressBar.Minimum = 0;
int max = 0;
foreach(ProjectItem item in project.Items)
{
if(item is FileProjectItem) {
max++;
}
}
progressBar.Maximum = max;
Controls.Add(progressBar);
cancelButton = new Button();
cancelButton.Location = new Point(188 + 80, 36);
cancelButton.Size = new Size(74, 23);
cancelButton.Text = ResourceService.GetString("Global.CancelButtonText");
cancelButton.Click += new EventHandler(StopThread);
Controls.Add(cancelButton);
}
public ExportProjectToHtmlDialog(IProject project)
{
this.project = project;
pathLabel.Location = new Point(8, 8);
pathLabel.Size = new Size(60,16);
pathLabel.Text = ResourceService.GetString("Dialog.ExportProjectToHtml.FolderLabel");
Controls.Add(pathLabel);
pathTextBox.Location = new Point(70, 8);
pathTextBox.Size = new Size(230,16);
Controls.Add(pathTextBox);
browseButton.Location = new Point(310, 8);
browseButton.Size = new Size(32,23);
browseButton.Text = "...";
browseButton.Click += new EventHandler(BrowseDirectories);
browseButton.FlatStyle = FlatStyle.System;
Controls.Add(browseButton);
okButton.Location = new Point(188, 36);
okButton.Size = new Size(74, 23);
okButton.Text = ResourceService.GetString("Global.OKButtonText");
okButton.Click += new EventHandler(ExportProject);
okButton.FlatStyle = FlatStyle.System;
Controls.Add(okButton);
cancelButton.Location = new Point(188 + 80, 36);
cancelButton.Size = new Size(74, 23);
cancelButton.Text = ResourceService.GetString("Global.CancelButtonText");
cancelButton.DialogResult = DialogResult.Cancel;
cancelButton.FlatStyle = FlatStyle.System;
Controls.Add(cancelButton);
Icon = null;
FormBorderStyle = FormBorderStyle.FixedDialog;
Size = new Size(350, 88 + 6);
StartPosition = FormStartPosition.CenterParent;
ShowInTaskbar = false;
MinimizeBox = MaximizeBox = false;
Text = ResourceService.GetString("Dialog.ExportProjectToHtml.DialogName");
}
void BrowseDirectories(object sender, EventArgs e)
{
FolderDialog fd = new FolderDialog();
if(fd.DisplayDialog(ResourceService.GetString("Dialog.ExportProjectToHtml.SelectTargetDirInfo")) == DialogResult.OK) {
pathTextBox.Text = fd.Path;
}
}
Hashtable projectTable = new Hashtable();
Hashtable bitmapTable = new Hashtable();
int bitmapIconIndex = 0;
int indexFileIndex = 0;
public Hashtable GetPath(string filename, Hashtable table, bool create)
{
string directory = Path.GetDirectoryName(filename);
string[] treepath = directory.Split(new char[] { Path.DirectorySeparatorChar });
Hashtable curTable = table;
foreach (string path in treepath) {
if (path.Length == 0 || path[0] == '.')
continue;
object node = curTable[path];
if (node == null) {
if (create) {
Hashtable newTable = new Hashtable();
curTable[path] = newTable;
curTable = newTable;
continue;
} else {
return null;
}
}
curTable = (Hashtable)node;
}
return curTable;
}
class Descriptor
{
public string title;
public string url;
public Descriptor(string title, string url)
{
this.title = title;
this.url = url;
}
}
StreamWriter curFileStream = null;
Stack curIndexStreamStack = new Stack();
int curSpanNumber = 0;
Hashtable Spans = new Hashtable();
string ExportFile(string fileName, string targetPath)
{
string targetFile;
if (fileName.EndsWith(".txt"))
{
targetFile = FileUtility.GetRelativePath(project.Directory, fileName).Substring(2);
File.Copy(fileName, targetPath + Path.DirectorySeparatorChar + targetFile);
return targetFile;
}
targetFile = FileUtility.GetRelativePath(project.Directory, fileName).Substring(2).Replace(Path.DirectorySeparatorChar.ToString(), "") + ".html";
IDocument document = new DocumentFactory().CreateDocument();
document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategyForFile(fileName);
StreamReader stream = File.OpenText(fileName);
document.TextContent = stream.ReadToEnd();
stream.Close();
curFileStream = File.CreateText(targetPath + Path.DirectorySeparatorChar + targetFile);
curFileStream.Write("<html>\r\n");
curFileStream.Write("<head>\r\n");
curFileStream.Write("<link rel=\"stylesheet\" type=\"text/css\" href=\"sdcss.css\">\r\n");
curFileStream.Write("<title>" + Path.GetFileName(fileName) + "</title>\r\n");
curFileStream.Write("</head>\r\n");
curFileStream.Write("<body>\r\n");
curFileStream.Write("<div class=\"code\"><TABLE SUMMARY=\"SourceCode\" BORDER=0 CELLSPACING=0 CELLPADDING=2 WIDTH=\"100%\">\r\n");
curFileStream.Write(" <TR BGCOLOR=\"#FFFFFF\">\r\n");
curFileStream.Write(" <TH WIDTH=\"50\" NOWRAP ALIGN=LEFT></TH>\r\n");
curFileStream.Write(" <TH WIDTH=\"1%\" NOWRAP ALIGN=LEFT></TH>\r\n");
curFileStream.Write(" <TH VALIGN=TOP ALIGN=LEFT> \r\n");
curFileStream.Write(" </TH>\r\n");
curFileStream.Write(" </TR>\r\n");
int i = 0;
foreach (LineSegment line in document.LineSegmentCollection) {
curFileStream.Write(" <TR BGCOLOR=\"#FFFFFF\" VALIGN=TOP>\r\n");
curFileStream.Write(" <TD NOWRAP VALIGN=TOP ALIGN=RIGHT>" + ++i + ":</TD>\r\n");
curFileStream.Write(" <TD NOWRAP VALIGN=TOP ALIGN=LEFT> \r\n");
bool spanOpen = false;
Color curColor = Color.Black;
bool oldItalic = false;
bool oldBold = false;
bool firstSpan = true;
foreach (TextWord word in line.Words) {
switch (word.Type) {
case TextWordType.Space:
curFileStream.Write(" ");
break;
case TextWordType.Tab:
for (int k = 0; k < document.TextEditorProperties.TabIndent; ++k) {
curFileStream.Write(" ");
}
break;
case TextWordType.Word:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -