Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
HtmlxToHtml.cs
1using System;
3using System.IO;
4using System.Threading.Tasks;
5using Waher.Content;
9using Waher.Events;
13using Waher.Script;
14
16{
21 {
25 public HtmlxToHtml()
26 {
27 }
28
32 public string[] FromContentTypes => new string[] { HtmlxDecoder.DefaultContentType };
33
37 public string[] ToContentTypes => new string[] { HtmlCodec.DefaultContentType };
38
42 public Grade ConversionGrade => Grade.Perfect;
43
49 public async Task<bool> ConvertAsync(ConversionState State)
50 {
51 string Htmlx;
52
53 using (StreamReader rd = new StreamReader(State.From))
54 {
55 Htmlx = await rd.ReadToEndAsync();
56 }
57
58 if (!(State.Progress is null))
59 {
60 string Head = HtmlDocument.GetHead(Htmlx);
61 if (!string.IsNullOrEmpty(Head))
62 {
63 try
64 {
65 HtmlDocument Doc = new HtmlDocument("<html><head>" + Head + "</head><body></body></html>");
66
67 if (Doc.Head?.HasChildren ?? false)
68 {
69 foreach (HtmlNode N in Doc.Head.Children)
70 {
71 if (!(N is HtmlElement E))
72 continue;
73
74 switch (E.LocalName.ToUpper())
75 {
76 case "LINK":
77 string Rel = E.GetAttribute("rel");
78 if (Rel == "stylesheet")
79 {
80 string HRef = E.GetAttribute("href");
81
82 if (!string.IsNullOrEmpty(HRef))
83 {
84 await State.Progress.EarlyHint(HRef, "preload",
85 new KeyValuePair<string, string>("as", "style"));
86 }
87 }
88 break;
89
90 case "SCRIPT":
91 string Type = E.GetAttribute("type");
93 {
94 string Src = E.GetAttribute("src");
95
96 if (!string.IsNullOrEmpty(Src))
97 {
98 await State.Progress.EarlyHint(Src, "preload",
99 new KeyValuePair<string, string>("as", "script"));
100 }
101 }
102 break;
103 }
104 }
105 }
106 }
107 catch (Exception ex)
108 {
109 Log.Exception(ex);
110 }
111 }
112
113 await State.Progress.HeaderProcessed();
114 }
115
116 string Html = await Convert(Htmlx, State, State.FromFileName);
117 if (State.HasError)
118 return false;
119
120 if (!(State.Progress is null))
121 await State.Progress.BodyProcessed();
122
123 byte[] Data = Strings.Utf8WithBom.GetBytes(Html);
124 await State.To.WriteAsync(Data, 0, Data.Length);
125 State.ToContentType += "; charset=utf-8";
126
127 return false;
128 }
129
137 public static Task<string> Convert(string Htmlx, ConversionState State, string FileName)
138 {
139 return Convert(Htmlx, State, State.Session, FileName, true);
140 }
141
149 public static Task<string> Convert(string Htmlx, Variables Session, string FileName)
150 {
151 return Convert(Htmlx, null, Session, FileName, true);
152 }
153
164 internal static Task<string> Convert(string Htmlx, ConversionState State, Variables Session, string FileName, bool LockSession)
165 {
166 return CssxToCss.Convert(Htmlx, "{{", "}}", State, Session, FileName, LockSession);
167 }
168
169 }
170}
Contains the state of a content conversion process.
Variables Session
Session states.
bool HasError
If an error should be returned.
Stream To
Stream pointing to where binary representation of content is to be sent.
string FromFileName
If the content is coming from a file, this parameter contains the name of that file....
ICodecProgress Progress
Optional interface for reporting progress during conversion.
Stream From
Stream pointing to binary representation of content.
static string GetHead(string Html)
Extracts the contents of the HEAD element in a HTML string.
Head Head
First HEAD element of document, if found, null otherwise.
Base class for all HTML elements.
Definition: HtmlElement.cs:13
bool HasChildren
If the element has children.
Definition: HtmlElement.cs:105
IEnumerable< HtmlNode > Children
Child nodes, or null if none.
Definition: HtmlElement.cs:115
Base class for all HTML nodes.
Definition: HtmlNode.cs:11
const string DefaultContentType
application/javascript
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:14
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1657
Converts CSSX-files to CSS, by evaluating emebedded script and replacing it with results.
Definition: CssxToCss.cs:21
static Task< string > Convert(string Cssx, ConversionState State, string FileName)
Converts CSSX to CSS, using the current theme
Definition: CssxToCss.cs:76
Converts HTMLX-files to HTML, by evaluating emebedded script and replacing it with results.
Definition: HtmlxToHtml.cs:21
async Task< bool > ConvertAsync(ConversionState State)
Performs the actual conversion.
Definition: HtmlxToHtml.cs:49
Grade ConversionGrade
How well the content is converted.
Definition: HtmlxToHtml.cs:42
static Task< string > Convert(string Htmlx, Variables Session, string FileName)
Converts HTMLX to HTML, using the current theme
Definition: HtmlxToHtml.cs:149
string[] FromContentTypes
Converts content from these content types.
Definition: HtmlxToHtml.cs:32
HtmlxToHtml()
Converts HTMLX-files to HTML, by evaluating emebedded script and replacing it with results.
Definition: HtmlxToHtml.cs:25
static Task< string > Convert(string Htmlx, ConversionState State, string FileName)
Converts HTMLX to HTML, using the current theme
Definition: HtmlxToHtml.cs:137
string[] ToContentTypes
Converts content to these content types.
Definition: HtmlxToHtml.cs:37
Static class managing binary representations of strings.
Definition: Strings.cs:10
static Encoding Utf8WithBom
UTF-8 encoding with Byte Order Mark (BOM)
Definition: Strings.cs:231
Collection of variables.
Definition: Variables.cs:25
Task HeaderProcessed()
Called when the header has been processed.
Task EarlyHint(string Resource, string Relation, params KeyValuePair< string, string >[] AdditionalParameters)
Reports an early hint of a resource the recipient may need to process, in order to be able to process...
Task BodyProcessed()
Called when the body has been processed.
Basic interface for Internet Content encoders. A class implementing this interface and having a defau...
Grade
Grade enumeration
Definition: Grade.cs:7