Skip to content

Commit

Permalink
Add existing code for HelpConvert project.
Browse files Browse the repository at this point in the history
  • Loading branch information
fancidev committed May 3, 2014
1 parent de7e0ef commit 91ad649
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 3 deletions.
6 changes: 6 additions & 0 deletions DosHelp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QuickHelp", "QuickHelp\Quic
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HelpBrowser", "HelpBrowser\HelpBrowser.csproj", "{00E978CD-7420-40EA-91A2-18E0F3CFF5C1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HelpConvert", "HelpConvert\HelpConvert.csproj", "{469806C9-9B3B-4AA2-94D9-35A2143FC585}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{00E978CD-7420-40EA-91A2-18E0F3CFF5C1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{00E978CD-7420-40EA-91A2-18E0F3CFF5C1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{00E978CD-7420-40EA-91A2-18E0F3CFF5C1}.Release|Any CPU.Build.0 = Release|Any CPU
{469806C9-9B3B-4AA2-94D9-35A2143FC585}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{469806C9-9B3B-4AA2-94D9-35A2143FC585}.Debug|Any CPU.Build.0 = Debug|Any CPU
{469806C9-9B3B-4AA2-94D9-35A2143FC585}.Release|Any CPU.ActiveCfg = Release|Any CPU
{469806C9-9B3B-4AA2-94D9-35A2143FC585}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 6 additions & 0 deletions HelpConvert/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>

<supportedRuntime version="v2.0.50727"/></startup>
</configuration>
59 changes: 59 additions & 0 deletions HelpConvert/HelpConvert.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{469806C9-9B3B-4AA2-94D9-35A2143FC585}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>HelpConvert</RootNamespace>
<AssemblyName>HelpConvert</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\QuickHelp\QuickHelp.csproj">
<Project>{c18c4ed1-e4b6-4dc3-b7cf-55053fec165b}</Project>
<Name>QuickHelp</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
144 changes: 144 additions & 0 deletions HelpConvert/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
using System;
using System.IO;
using System.Text;
using QuickHelp;
using QuickHelp.Converters;
using QuickHelp.Serialization;

namespace HelpConvert
{
class Program
{
static void Main(string[] args)
{
Convert(args, false);

#if DEBUG
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
#endif
}

static void PrintUsage()
{
Console.WriteLine("HelpConvert input-file [input-file ...]");
}

static void Convert(string[] fileNames, bool isDryRun)
{
HelpSystem system = new HelpSystem();
BatchHtmlConverter converter = new BatchHtmlConverter(system);

foreach (string fileName in fileNames)
{
var decoder = new DatabaseDecoder();
foreach (HelpDatabase database in decoder.LoadDatabases(fileName))
system.Databases.Add(database);
}

// export HTML
foreach (HelpDatabase database in system.Databases)
{
// TODO: check invalid chars in database name
string htmlPath = database.Name.Replace('.', '_');
Directory.CreateDirectory(htmlPath);

int topicIndex = 0;
foreach (HelpTopic topic in database.Topics)
{
string html = converter.ConvertTopic(topic);
string htmlFileName = Path.Combine(htmlPath, string.Format("T{0:X4}.html", topicIndex));
if (!isDryRun)
{
using (StreamWriter writer = new StreamWriter(htmlFileName, false, Encoding.UTF8))
{
writer.Write(html);
}
}
topicIndex++;
}

// Create contents.html.
HelpTopic topic1 = system.ResolveUri(database, new HelpUri("h.contents"));
if (topic1 != null && topic1.Database == database)
{
if (!isDryRun)
{
using (StreamWriter writer = new StreamWriter(Path.Combine(htmlPath, "Contents.html")))
{
writer.WriteLine("<meta http-equiv=\"refresh\" content=\"0; url=T{0:X4}.html\">",
topic1.TopicIndex);
}
}
}
}
}

static string GetDatabasePath(HelpDatabase database)
{
string path = database.Name.Replace('.', '_');
Directory.CreateDirectory(path);
return path;
}
}

class BatchHtmlConverter : HtmlConverter
{
readonly HelpSystem system;

public BatchHtmlConverter(HelpSystem system)
{
base.AutoFixHyperlinks = true;
this.system = system;
}

protected override string ConvertUri(HelpTopic source, HelpUri uri)
{
switch (uri.Type)
{
case HelpUriType.Context:
case HelpUriType.GlobalContext:
case HelpUriType.LocalContext:
{
HelpTopic target = system.ResolveUri(source.Database, uri);
if (target != null)
{
if (target.Database == source.Database)
{
return string.Format("T{0:X4}.html", target.TopicIndex);
}
else
{
return string.Format("../{0}/T{1:X4}.html",
GetDatabasePath(target.Database),
target.TopicIndex);
}
}
else
{
Console.WriteLine("Warning: cannot resolve context string '{0}'", uri);
}
}
break;

case HelpUriType.TopicIndex:
return string.Format("T{0:X4}.html", uri.TopicIndex);

case HelpUriType.Command:
case HelpUriType.File:
default:
// TODO: would be better if we have the source location.
Console.WriteLine("Warning: cannot convert link: {0}", uri);
break;
}
return "?" + uri.ToString();
}

static string GetDatabasePath(HelpDatabase database)
{
string path = database.Name.Replace('.', '_');
Directory.CreateDirectory(path);
return path;
}
}
}
36 changes: 36 additions & 0 deletions HelpConvert/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("HelpConvert")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("HelpConvert")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("a2d32d66-6dd2-4bbf-a238-e9e719544a6b")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// 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("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
6 changes: 3 additions & 3 deletions QuickHelp/Converters/HtmlConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private void FormatLine(StringBuilder html, HelpTopic topic, HelpLine line)
if (newAttrs.Link != null)
{
html.AppendFormat("<a href=\"{0}\">",
ConvertLink(topic, newAttrs.Link));
ConvertUri(topic, newAttrs.Link));
}
}
html.Append(Escape("" + line.Text[i]));
Expand All @@ -113,9 +113,9 @@ private void FormatLine(StringBuilder html, HelpTopic topic, HelpLine line)
html.AppendLine();
}

protected virtual string ConvertLink(HelpTopic topic, HelpUri link)
protected virtual string ConvertUri(HelpTopic topic, HelpUri uri)
{
return "?" + Escape(link.ToString());
return "?" + Escape(uri.ToString());
}

private static void FormatAddedStyles(
Expand Down

0 comments on commit 91ad649

Please sign in to comment.