我正在尝试创建一个脚本,将 HTML 转换为 Jira 的 ADF 格式。当我尝试使用 Visual Studio 2019 测试脚本时,它仅给出此输出 - “程序 '[36520] HTMLtoADF.exe' 已退出,代码为 0”。我对 C# 或 Visual Studio 不太熟悉,所以任何建议将不胜感激。这是我在下面创建的脚本:
using System;
using System.Collections.Generic;
using HtmlAgilityPack;
using Newtonsoft.Json.Linq;
class Program
{
static void Main()
{
// Sample HTML content
string htmlContent = "<p>Hello, <strong>world!</strong></p>";
// Convert HTML to ADF
string adfResult = ConvertHtmlToAdf(htmlContent);
// Output the ADF result
Console.WriteLine("Converted ADF:");
Console.WriteLine(adfResult);
}
static string ConvertHtmlToAdf(string htmlContent)
{
// Load HTML content into an HtmlDocument for parsing
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(htmlContent);
// Create a list to store ADF nodes
List<JObject> adfNodes = new List<JObject>();
// Convert HTML nodes to ADF
foreach (var htmlNode in htmlDoc.DocumentNode.ChildNodes)
{
var adfNode = ConvertHtmlNodeToAdfNode(htmlNode);
adfNodes.Add(adfNode);
}
// Create an ADF document with the collected nodes
var adfDocument = new JObject(new JProperty("version", 1), new JProperty("type", "doc"), new JProperty("content", adfNodes));
// Return the ADF content as a JSON string
return adfDocument.ToString();
}
static JObject ConvertHtmlNodeToAdfNode(HtmlNode htmlNode)
{
// Map HTML node to ADF node
switch (htmlNode.NodeType)
{
// If the HTML node is an element (e.g., <p>, <strong>)
case HtmlNodeType.Element:
return ConvertHtmlElementToAdfNode(htmlNode);
// If the HTML node is a text node
case HtmlNodeType.Text:
return ConvertHtmlTextToAdfNode(htmlNode);
// Handle other node types as needed
default:
return new JObject();
}
}
static JObject ConvertHtmlElementToAdfNode(HtmlNode htmlElement)
{
// Map HTML element to ADF element
switch (htmlElement.Name.ToLower())
{
// If the HTML element is a paragraph (<p>)
case "p":
// Convert the <p> element to an ADF paragraph node
return new JObject(
new JProperty("type", "paragraph"),
new JProperty("content", new JArray(ConvertHtmlNodesToAdfNodes(htmlElement.ChildNodes))));
// If the HTML element is a strong emphasis (<strong>)
case "strong":
// Convert the <strong> element to an ADF text node with strong emphasis
return new JObject(
new JProperty("type", "text"),
new JProperty("text", htmlElement.InnerHtml),
new JProperty("marks", new JArray(new JObject(new JProperty("type", "strong")))));
// Add more cases for other HTML elements as needed
default:
return new JObject();
}
}
static JObject ConvertHtmlTextToAdfNode(HtmlNode htmlText)
{
// Map HTML text to ADF text
// Create an ADF text node with the content of the HTML text node
return new JObject(new JProperty("type", "text"), new JProperty("text", htmlText.InnerHtml));
}
static List<JObject> ConvertHtmlNodesToAdfNodes(HtmlNodeCollection htmlNodes)
{
// Convert a collection of HTML nodes to ADF nodes
var adfNodes = new List<JObject>();
foreach (var htmlNode in htmlNodes)
{
// Convert each HTML node to its equivalent ADF node
var adfNode = ConvertHtmlNodeToAdfNode(htmlNode);
adfNodes.Add(adfNode);
}
return adfNodes;
}
}
exited with code 0
表示您的程序完成了工作,没有错误。
运行程序时应该出现黑色的命令窗口。
但是如果您使用的是 C# .NET Framework 控制台项目,则黑色窗口将在完成后快速关闭(不到一秒)。
所以,你可以在main的最后一行添加
Console.ReadKey();
来防止窗口关闭。
static void Main()
{
// Sample HTML content
string htmlContent = "<p>Hello, <strong>world!</strong></p>";
// Convert HTML to ADF
string adfResult = ConvertHtmlToAdf(htmlContent);
// Output the ADF result
Console.WriteLine("Converted ADF:");
Console.WriteLine(adfResult);
Console.ReadKey();//added
}