public HttpCloneDocType this[string rawMime] { get { HttpCloneDocType info; if (!_map.TryGetValue(rawMime, out info)) { info = new HttpCloneDocType() { MimeType = rawMime, Type = ContentFormat.Unknown, FileExtension = null, Aliases = new HttpCloneDocType[0], }; _map.Add(rawMime, info); } if (String.IsNullOrEmpty(info.FileExtension)) { info.FileExtension = ".unk"; foreach (Match m in Regex.Matches(rawMime, @"^\w*/(?<type>\w*)$")) { info.FileExtension = '.' + m.Groups["type"].Value.ToLower(); } } return(info); } }
public HttpCloneDocType this[string rawMime] { get { HttpCloneDocType info; if (!_map.TryGetValue(rawMime, out info)) { info = new HttpCloneDocType() { MimeType = rawMime, Type = ContentFormat.Unknown, FileExtension = null, Aliases = new HttpCloneDocType[0], }; _map.Add(rawMime, info); } if (String.IsNullOrEmpty(info.FileExtension)) { info.FileExtension = ".unk"; foreach (Match m in Regex.Matches(rawMime, @"^\w*/(?<type>\w*)$")) info.FileExtension = '.' + m.Groups["type"].Value.ToLower(); } return info; } }
public string FromExtension(string extension) { HttpCloneDocType type = _config.DocumentTypes.FirstOrDefault( d => StringComparer.OrdinalIgnoreCase.Equals(d.FileExtension, extension) || null != d.Aliases.SafeEnumeration().FirstOrDefault( a => StringComparer.OrdinalIgnoreCase.Equals(a.FileExtension, extension)) ); if (type == null) { throw new ApplicationException("Unknown file type " + extension); } return(type.MimeType); }
private bool ProcessTagUri(string path, XmlLightElement child, IEnumerable <HttpCloneDocumentTag> tags, bool useRelativePaths, HttpCloneDocType docType) { Uri current = new Uri(_baseUri, path); bool modified = false; string value; foreach (HttpCloneDocumentTag tag in tags) { if (!IsTagMatch(child, tag)) { continue; } if (!String.IsNullOrEmpty(tag.ContentType)) { foreach (XmlLightElement eText in child.FindElement(x => x.TagName == XmlLightElement.TEXT || x.TagName == XmlLightElement.CDATA)) { value = eText.Value; bool isHtmlFragment = tag.ContentType == "text/html" && IsHtml.IsMatch(value) == false; if (isHtmlFragment) { value = "<html><body>" + value + "</body></html>"; } string newcontent = ProcessFileText(path, tag.ContentType, useRelativePaths, value); if (newcontent != value) { if (isHtmlFragment) { int ixStart = newcontent.IndexOf("<body>"); int ixEnd = newcontent.IndexOf("</body>"); if (ixStart < 0 || ixEnd < 0) { throw new ApplicationException("Unable to obtain html/body content."); } ixStart += "<body>".Length; newcontent = newcontent.Substring(ixStart, ixEnd - ixStart); } modified = true; eText.Value = newcontent; } } } if (!String.IsNullOrEmpty(tag.Attribute) && child.Attributes.ContainsKey(tag.Attribute)) { value = child.Attributes[tag.Attribute]; Uri uri; if (!value.StartsWith("#") && Uri.TryCreate(current, value, out uri)) { Uri rewrite = OnRewriteUri(uri); if (!ReferenceEquals(rewrite, uri) || useRelativePaths) { string relPath = rewrite.AbsoluteUri; if (useRelativePaths) { Uri cur = new Uri(OnRewriteUri(current), "./"); relPath = MakeRelativeUri(cur, rewrite).OriginalString; if (String.IsNullOrEmpty(relPath) || relPath[0] == '?') { relPath = "./" + relPath; } } child.Attributes[tag.Attribute] = relPath; modified = true; } } } foreach (HttpCloneTagAttribute atag in tag.Attributes.SafeEnumeration()) { if (!String.IsNullOrEmpty(atag.Name) && !String.IsNullOrEmpty(atag.ContentType) && child.Attributes.ContainsKey(atag.Name)) { string content = child.Attributes[atag.Name]; string replace = ProcessFileText(path, useRelativePaths, content, _documentTypes[atag.ContentType]); if (!ReferenceEquals(replace, content)) { child.Attributes[atag.Name] = replace; modified = true; } } } } return(modified); }
private bool Visit(string path, string mime, List <XmlLightElement> children, bool useRelativePaths, HttpCloneDocType docType) { bool modified = false; for (int i = 0; i < children.Count; i++) { XmlLightElement child = OnRewriteElement(children[i]); if (child == null) { children[i].Parent = null; children.RemoveAt(i); i--; modified = true; continue; } if (!ReferenceEquals(child, children[i])) { child.Parent = children[i].Parent; children[i].Parent = null; children[i] = child; modified = true; } List <HttpCloneDocumentTag> tags; if (_documentTags.TryGetValue(mime + MimeTagDivide + child.TagName, out tags)) { modified = ProcessTagUri(path, child, tags, useRelativePaths, docType) | modified; } modified = Visit(path, mime, child.Children, useRelativePaths, docType) | modified; if (docType.ProcessPlainTextLinks && child.IsText) { string content = child.OriginalTag; string newcontent = ProcessTextMatch(path, false, content, RegexPatterns.HttpUrl, 0); if (!ReferenceEquals(content, newcontent)) { child.OriginalTag = newcontent; modified = true; } } } return(modified); }
private string ProcessFileText(string path, bool useRelativePaths, string content, HttpCloneDocType docType) { if (docType.Type == ContentFormat.Text && docType.ProcessPlainTextLinks) { content = ProcessTextMatch(path, useRelativePaths, content, RegexPatterns.HttpUrl, 0); } foreach (HttpCloneMatch exp in docType.Matches.SafeEnumeration()) { content = ProcessTextMatch(path, useRelativePaths, content, new Regex(exp.Expression), exp.GroupId); } return(content); }