Ejemplo n.º 1
0
        public static XrefDetails From(HtmlAgilityPack.HtmlNode node)
        {
            if (node.Name != "xref") throw new NotSupportedException("Only xref node is supported!");
            var xref = new XrefDetails();
            xref.Uid = node.GetAttributeValue("href", null);
            var overrideName = node.InnerText;
            if (!string.IsNullOrEmpty(overrideName))
            {
                xref.AnchorDisplayName = overrideName;
                xref.PlainTextDisplayName = overrideName;
            }
            else
            {
                // If name | fullName exists, use the one from xref because spec name is different from name for generic types
                // e.g. return type: IEnumerable<T>, spec name should be IEnumerable
                xref.AnchorDisplayName = node.GetAttributeValue("name", null);
                xref.PlainTextDisplayName = node.GetAttributeValue("fullName", null);
            }

            xref.Title = node.GetAttributeValue("title", null);
            xref.Raw = node.GetAttributeValue("data-raw", null);
            xref.ThrowIfNotResolved = node.GetAttributeValue("data-throw-if-not-resolved", false);

            return xref;
        }
Ejemplo n.º 2
0
 private static void UpdateSrc(HtmlAgilityPack.HtmlNode link, Dictionary<string, string> map, Func<string, string> updater)
 {
     string attribute = "src";
     var key = link.GetAttributeValue(attribute, null);
     string path;
     if (PathUtility.TryGetPathFromWorkingFolder(key, out path))
     {
         string xrefValue;
         if (map.TryGetValue(key, out xrefValue))
         {
             xrefValue = updater(xrefValue);
             link.SetAttributeValue(attribute, xrefValue);
         }
         else
         {
             Logger.Log(LogLevel.Warning, $"File {path} is not found.");
             // TODO: what to do if file path not exists?
             // CURRENT: fallback to the original one
             link.SetAttributeValue(attribute, path);
         }
     }
 }
Ejemplo n.º 3
0
        private static void UpdateHref(HtmlAgilityPack.HtmlNode link, Dictionary<string, string> map, Func<string, string> updater)
        {
            string attribute = "href";
            var key = link.GetAttributeValue(attribute, null);
            string path;
            if (PathUtility.TryGetPathFromWorkingFolder(key, out path))
            {
                string href;
                // For href, # may be appended, remove # before search file from map
                var anchorIndex = key.IndexOf("#");
                var anchor = string.Empty;
                if (anchorIndex == 0) return;
                if (anchorIndex > 0)
                {
                    anchor = key.Substring(anchorIndex);
                    key = key.Remove(anchorIndex);
                }

                if (map.TryGetValue(key, out href))
                {
                    href = updater(href);
                    href += anchor;
                    link.SetAttributeValue(attribute, href);
                }
                else
                {
                    Logger.Log(LogLevel.Warning, $"File {path} is not found.");
                    // TODO: what to do if file path not exists?
                    // CURRENT: fallback to the original one
                    link.SetAttributeValue(attribute, path);
                }
            }
        }
Ejemplo n.º 4
0
        private static void UpdateXref(HtmlAgilityPack.HtmlNode xref, Dictionary<string, XRefSpec> internalXRefMap, Dictionary<string, XRefSpec> externalXRefMap, Func<string, string> updater, string language)
        {
            var key = xref.GetAttributeValue("href", null);
            // If name | fullName exists, use the one from xref because spec name is different from name for generic types
            // e.g. return type: IEnumerable<T>, spec name should be IEnumerable
            var name = xref.GetAttributeValue("name", null);
            var fullName = xref.GetAttributeValue("fullName", null);
            string displayName;
            string href = null;

            XRefSpec spec = null;
            if (internalXRefMap.TryGetValue(key, out spec))
            {
                spec.Href = updater(spec.Href);
                var hashtagIndex = spec.Href.IndexOf('#');
                if (hashtagIndex == -1)
                {
                    var htmlId = GetHtmlId(key);
                    // TODO: What if href is not html?
                    spec.Href = spec.Href + "#" + htmlId;
                }

                href = spec.Href;
            }
            else if (externalXRefMap.TryGetValue(key, out spec) && !string.IsNullOrEmpty(spec.Href))
            {
                href = spec.Href;
            }

            // If href is not null, use name
            if (href != null)
            {
                if (!string.IsNullOrEmpty(name))
                {
                    displayName = name;
                }
                else
                {
                    displayName = string.IsNullOrEmpty(fullName) ? key : fullName;
                    if (spec != null)
                        displayName = StringHelper.HtmlEncode(GetLanguageSpecificAttribute(spec, language, displayName, "name"));
                }

                var anchorNode = $"<a class=\"xref\" href=\"{href}\">{displayName}</a>";
                xref.ParentNode.ReplaceChild(HtmlAgilityPack.HtmlNode.CreateNode(anchorNode), xref);
            }
            else
            {
                // If href is null, use fullName
                if (!string.IsNullOrEmpty(fullName))
                {
                    displayName = fullName;
                }
                else
                {
                    displayName = string.IsNullOrEmpty(name) ? key : name;
                    if (spec != null)
                        displayName = StringHelper.HtmlEncode(GetLanguageSpecificAttribute(spec, language, displayName, "fullName", "name"));
                }

                var spanNode = $"<span class=\"xref\">{displayName}</span>";
                xref.ParentNode.ReplaceChild(HtmlAgilityPack.HtmlNode.CreateNode(spanNode), xref);
            }
        }
Ejemplo n.º 5
0
 private string[] ProcessNode(HtmlAgilityPack.HtmlNode htmlNode)
 {
     if (htmlNode == null || string.IsNullOrWhiteSpace(htmlNode.Name))
         return null;
     string nodeName = htmlNode.Name.ToLower().Trim();
     switch (nodeName)
     {
         case "a":
             {
                 string href = htmlNode.GetAttributeValue("href", string.Empty);
                 if (!string.IsNullOrWhiteSpace(href))
                 {
                     string url = ProcessUrl(href, false);
                     if (!string.IsNullOrWhiteSpace(url))
                         return new string[] { url };
                 }
                 else if(_isLiveATC)
                 {
                     string onClick = htmlNode.GetAttributeValue("onClick", string.Empty);
                     if (!string.IsNullOrWhiteSpace(onClick) && onClick.ToLower().Contains("mydirectstream") && onClick.IndexOf('\'') > 0)
                     {
                         onClick = onClick.Substring(onClick.IndexOf('\'') + 1);
                         if (!string.IsNullOrWhiteSpace(onClick) && onClick.IndexOf('\'') > 0)
                         {
                             onClick = onClick.Substring(0, onClick.IndexOf('\''));
                             string url = ProcessUrl(string.Format("http://d.liveatc.net/{0}.m3u", onClick), false);
                             if (!string.IsNullOrWhiteSpace(url))
                                 return new string[] { url };
                         }
                     }
                 }
                 break;
             }
         case "meta":
             {
                 string metaType = htmlNode.GetAttributeValue("http-equiv", string.Empty);
                 if (string.Equals(metaType, "refresh", StringComparison.InvariantCultureIgnoreCase))
                 {
                     string metaContent = htmlNode.GetAttributeValue("content", string.Empty);
                     if (!string.IsNullOrWhiteSpace(metaContent))
                     {
                         int indUrl = metaContent.ToLower().IndexOf("url=");
                         if (indUrl >= 0)
                         {
                             metaContent = metaContent.Substring(indUrl + 4);
                         }
                         string url = ProcessUrl(metaContent, false);
                         if (!string.IsNullOrWhiteSpace(url))
                             return new string[] { url };
                     }
                 }
                 break;
             }
         case "object":
             {
                 string classid = htmlNode.GetAttributeValue("classid", string.Empty);
                 if (string.Equals(classid, "CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95", StringComparison.InvariantCultureIgnoreCase))
                 {
                     //this is a media player object...
                     string mediaURL = string.Empty;
                     foreach (HtmlAgilityPack.HtmlNode subNode in htmlNode.ChildNodes)
                     {
                         string subNodeName = subNode.Name.ToLower().Trim();
                         if(string.Equals(subNodeName,"param", StringComparison.InvariantCultureIgnoreCase) && string.Equals(subNode.GetAttributeValue("name",string.Empty), "filename", StringComparison.InvariantCultureIgnoreCase))
                         {
                             string strFileName = subNode.GetAttributeValue("value", string.Empty);
                             if(!string.IsNullOrWhiteSpace(strFileName))
                             {
                                 return new string[] { strFileName };
                             }
                         }
                     }
                 }
                 break;
             }
         case "script":
             {
                 if (string.IsNullOrWhiteSpace(htmlNode.GetAttributeValue("src", string.Empty)) && !string.IsNullOrWhiteSpace(htmlNode.InnerText))
                 {
                     //process script...
                     System.IO.StringReader rdr = new System.IO.StringReader(htmlNode.InnerText);
                     string inLine=string.Empty;
                     do
                     {
                         inLine = rdr.ReadLine();
                         if (!string.IsNullOrWhiteSpace(inLine))
                         {
                             inLine = inLine.ToLower();
                             int indx = inLine.IndexOf(".playlist");
                             if (indx >= 0)
                             {
                                 indx += 9;
                                 inLine = inLine.Substring(indx);
                                 indx = inLine.IndexOf('\'');
                                 if (indx >= 0)
                                 {
                                     inLine = inLine.Substring(indx + 1);
                                     indx = inLine.IndexOf('\'');
                                     if (indx >= 0)
                                     {
                                         inLine = GetFeedUrlFromValue(inLine.Substring(0, indx), true);
                                         if (!string.IsNullOrWhiteSpace(inLine))
                                         {
                                             return new string[] { inLine };
                                         }
                                     }
                                 }
                             }
                             else
                             {
                                 indx = inLine.IndexOf("ipadurl");
                                 if (indx >= 0)
                                 {
                                     indx += 7;
                                     inLine = inLine.Substring(indx);
                                     indx = inLine.IndexOf("\"");
                                     if (indx < 0)
                                         indx = inLine.IndexOf("'");
                                     if (indx >= 0)
                                     {
                                         inLine = inLine.Substring(indx + 1);
                                         indx = inLine.LastIndexOf("\"");
                                         if (indx < 0)
                                             indx = inLine.LastIndexOf("'");
                                         if (indx >= 0)
                                         {
                                             inLine = GetFeedUrlFromValue(inLine.Substring(0, indx), true);
                                             if (!string.IsNullOrWhiteSpace(inLine))
                                             {
                                                 return new string[] { inLine };
                                             }
                                         }
                                     }
                                 }
                                 else
                                 {
                                     indx = inLine.IndexOf(".m3u");
                                     if (indx >= 0)
                                     {
                                         int indxStart = inLine.IndexOf('"');
                                         int indxEnd = inLine.LastIndexOf('"');
                                         if (indxEnd > 0)
                                             inLine = inLine.Substring(0, indxEnd);
                                         if (indxStart >= 0)
                                             inLine = inLine.Substring(indxStart + 1);
                                         if (_isBroadcastify && inLine.IndexOf('"') >= 0)
                                         {
                                             indxStart = inLine.IndexOf('"');
                                             indxEnd = inLine.LastIndexOf('"');
                                             string tmpStart = inLine.Substring(0, indxStart);
                                             string tmpEnd = inLine.Substring(indxEnd + 1);
                                             if (string.IsNullOrWhiteSpace(_broadcastifyFeedId))
                                             {
                                                 _broadcastifyListPrefix = tmpStart;
                                                 _broadcastifyListSuffix = tmpEnd;
                                             }
                                             else
                                             {
                                                 inLine = tmpStart + _broadcastifyFeedId + tmpEnd;
                                                 if (!inLine.StartsWith("/"))
                                                     inLine = _broadcastifyPrefix + "/" + inLine;
                                                 else
                                                     inLine = _broadcastifyPrefix + inLine;
                                                 if (!string.IsNullOrWhiteSpace(inLine))
                                                 {
                                                     return new string[] { inLine };
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                     while (inLine != null);
                 }
                 break;
             }
         default:
             {
                 if (htmlNode.HasChildNodes)
                 {
                     List<string> subLinks = new List<string>();
                     foreach (HtmlAgilityPack.HtmlNode subNode in htmlNode.ChildNodes)
                     {
                         string[] links = ProcessNode(subNode);
                         if (links != null)
                         {
                             foreach (string strLink in links)
                             {
                                 if (!subLinks.Contains(strLink))
                                     subLinks.Add(strLink);
                             }
                         }
                     }
                     PostProcessBroadcastifyFeeds(subLinks);
                     return subLinks.ToArray();
                 }
                 break;
             }
     }
     return null;
 }
 private void addTableRow(HtmlAgilityPack.HtmlNode node)
 {
     dt.Rows.Add(node.ChildNodes[0].InnerHtml, node.GetAttributeValue("href", ""), "");
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Prepare Html
        /// </summary>
        /// <param name="input"></param>
        /// <param name="output"></param>
        private void PrepareHtml(HtmlAgilityPack.HtmlNode input, HtmlAgilityPack.HtmlNode output)
        {
            HtmlAgilityPack.HtmlNode parent = output;

            switch (input.NodeType)
            {
                case HtmlAgilityPack.HtmlNodeType.Document:
                    break;
                case HtmlAgilityPack.HtmlNodeType.Element:

                    if (!availableTags.Contains(input.OriginalName))
                        return;

                    if (input.OriginalName.Equals("img"))
                    {
                        output.AppendChild(input.CloneNode(true));
                        return;
                    }

                    parent = output.AppendChild(input.CloneNode(false));

                    string newStyle = string.Empty;
                    string style = input.GetAttributeValue("style", string.Empty);

                    string href = input.GetAttributeValue("href", string.Empty);

                    parent.Attributes.RemoveAll();

                    if (style != string.Empty)
                        foreach (string item in style.Split(';'))
                            if ((item.Contains("font-weight") && item.Contains("bold")) ||
                                (item.Contains("font-style") && item.Contains("italic")) ||
                                (item.Contains("text-decoration") && item.Contains("underline")))
                                newStyle += string.Format("{0};", item);

                    if (newStyle != string.Empty)
                        parent.SetAttributeValue("style", newStyle);

                    if (href != string.Empty)
                        parent.SetAttributeValue("href", href);

                    break;
                case HtmlAgilityPack.HtmlNodeType.Text:
                    output.AppendChild(input.CloneNode(true));
                    return;
                default:
                    return;
            }

            foreach (HtmlAgilityPack.HtmlNode child in input.ChildNodes)
                PrepareHtml(child, parent);
        }
Ejemplo n.º 8
0
        public static XRefDetails From(HtmlAgilityPack.HtmlNode node)
        {
            if (node.Name != "xref") throw new NotSupportedException("Only xref node is supported!");
            var xref = new XRefDetails();

            var rawUid = GetRawUid(node);
            NameValueCollection queryString = null;
            if (!string.IsNullOrEmpty(rawUid))
            {
                string others;
                var anchorIndex = rawUid.IndexOf("#");
                if (anchorIndex == -1)
                {
                    xref.Anchor = string.Empty;
                    others = rawUid;
                }
                else
                {
                    xref.Anchor = rawUid.Substring(anchorIndex);
                    others = rawUid.Remove(anchorIndex);
                }
                var queryIndex = others.IndexOf("?");
                if (queryIndex == -1)
                {
                    xref.Uid = HttpUtility.UrlDecode(others);
                }
                else
                {
                    xref.Uid = HttpUtility.UrlDecode(others.Remove(queryIndex));
                    queryString = HttpUtility.ParseQueryString(others.Substring(queryIndex));
                }
            }

            xref.InnerHtml = node.InnerHtml;
            xref.DisplayProperty = node.GetAttributeValue("displayProperty", queryString?.Get("displayProperty") ?? XRefSpec.NameKey);
            xref.AltProperty = node.GetAttributeValue("altProperty", queryString?.Get("altProperty") ?? "fullName");
            xref.Text = node.GetAttributeValue("text", node.GetAttributeValue("name", StringHelper.HtmlEncode(queryString?.Get("text"))));
            xref.Alt = node.GetAttributeValue("alt", node.GetAttributeValue("fullname", StringHelper.HtmlEncode(queryString?.Get("alt"))));

            xref.Title = node.GetAttributeValue("title", queryString?.Get("title"));
            xref.SourceFile = node.GetAttributeValue("sourceFile", null);
            xref.SourceStartLineNumber = node.GetAttributeValue("sourceStartLineNumber", 0);
            xref.SourceEndLineNumber = node.GetAttributeValue("sourceEndLineNumber", 0);

            // Both `data-raw-html` and `data-raw-source` are html encoded. Use `data-raw-html` with higher priority.
            // `data-raw-html` will be decoded then displayed, while `data-raw-source` will be displayed directly.
            xref.RawSource = node.GetAttributeValue("data-raw-source", null);
            var raw = node.GetAttributeValue("data-raw-html", null);
            if (!string.IsNullOrEmpty(raw))
            {
                xref.Raw = StringHelper.HtmlDecode(raw);
            }
            else
            {
                xref.Raw = xref.RawSource;
            }

            xref.ThrowIfNotResolved = node.GetAttributeValue("data-throw-if-not-resolved", false);

            return xref;
        }
Ejemplo n.º 9
0
 private static string GetRawUid(HtmlAgilityPack.HtmlNode node)
 {
     string href = node.GetAttributeValue("href", null);
     if (!string.IsNullOrEmpty(href))
     {
         return href;
     }
     return node.GetAttributeValue("uid", null);
 }
Ejemplo n.º 10
0
 public string SetTagData(string content, HtmlAgilityPack.HtmlNode data)
 {
     string ret = "";
     if (content == "CreateTag") {
         string tagName = data.GetAttributeValue("tagName", null).DecodeXMLString();
         string tagIdStr = data.GetAttributeValue("tagIdStr", null).DecodeXMLString();
         int parentTagId = data.GetAttributeValue("parentTagId", -1);
         int tagId = CreateTag(tagName, tagIdStr, parentTagId);
         TagInfoBase tag = GetTagInfo(tagId);
         if (tag != null)
             return tag.ToXML();
     }
     else if (content == "DeleteTag") {
         int tagId = data.GetAttributeValue("tagId", -1);
         DeleteTag(tagId);
     }
     else if (content == "RenameTag") {
         int tagId = data.GetAttributeValue("tagId", -1);
         string tagName = data.GetAttributeValue("tagName", null).DecodeXMLString();
         RenameTag(tagId, tagName);
     }
     else if (content == "AssignTag") {
         int tagId = data.GetAttributeValue("tagId", -1);
         string itemIds = data.GetAttributeValue("itemIds", "");
         IEnumerable<int> itemIdList = from item in itemIds.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries) select int.Parse(item);
         MinerSetTagForItems(tagId, itemIdList);
     }
     else if (content == "RemoveTag") {
         int tagId = data.GetAttributeValue("tagId", -1);
         string itemIds = data.GetAttributeValue("itemIds", "");
         IEnumerable<int> itemIdList = from item in itemIds.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries) select int.Parse(item);
         MinerRemoveTagForItems(tagId, itemIdList);
     }
     return ret;
 }
Ejemplo n.º 11
0
 public string RequestTagData(string content, HtmlAgilityPack.HtmlNode dataQueryNode)
 {
     StringBuilder result = new StringBuilder();
     if (content == "GetItemsForTagId") {
         int tagId = dataQueryNode.GetAttributeValue("tagId", -1);
         return MinerGetItemsForTagId(tagId);
     }
     else if (content == "GetTagsForItemId") {
         int itemId = dataQueryNode.GetAttributeValue("itemId", -1);
         return MinerGetTagsForItemId(itemId);
     }
     return null;
 }
Ejemplo n.º 12
0
        private static void UpdateHref(HtmlAgilityPack.HtmlNode link, string attribute, IDocumentBuildContext context, string relativePath)
        {
            var originalHref = link.GetAttributeValue(attribute, null);
            var anchor = link.GetAttributeValue("anchor", null);
            link.Attributes.Remove("anchor");
            string href;
            var path = RelativePath.TryParse(originalHref);

            if (path?.IsFromWorkingFolder() == true)
            {
                var targetPath = (RelativePath)context.GetFilePath(path.UrlDecode());

                if (targetPath != null)
                {
                    href = (targetPath.RemoveWorkingFolder() - (RelativePath)relativePath).UrlEncode();
                }
                else
                {
                    Logger.LogInfo($"File {path} is not found in {relativePath}.");
                    // TODO: what to do if file path not exists?
                    // CURRENT: fallback to the original one
                    href = (path.UrlDecode().RemoveWorkingFolder() - (RelativePath)relativePath).UrlEncode();
                }
                link.SetAttributeValue(attribute, href + anchor);
            }
        }
Ejemplo n.º 13
-1
        public static HtmlAgilityPack.HtmlNode ConvertXrefLinkNodeToXrefNode(HtmlAgilityPack.HtmlNode node)
        {
            var href = node.GetAttributeValue("href", null);
            if (node.Name != "a" || string.IsNullOrEmpty(href) || !href.StartsWith("xref:"))
            {
                throw new NotSupportedException("Only anchor node with href started with \"xref:\" is supported!");
            }
            href = href.Substring("xref:".Length);
            var raw = StringHelper.HtmlEncode(node.OuterHtml);

            var xrefNode = $"<xref href=\"{href}\" data-throw-if-not-resolved=\"True\" data-raw-html=\"{raw}\"";
            foreach (var attr in node.Attributes ?? Enumerable.Empty<HtmlAgilityPack.HtmlAttribute>())
            {
                if (attr.Name == "href" || attr.Name == "data-throw-if-not-resolved" || attr.Name == "data-raw-html")
                {
                    continue;
                }
                xrefNode += $" {attr.Name}=\"{attr.Value}\"";
            }
            xrefNode += $">{node.InnerText}</xref>";

            return HtmlAgilityPack.HtmlNode.CreateNode(xrefNode);
        }