public static TModel FromHTMLContent <TModel>(HTMLContent htmlContent) where
        TModel : HTMLContentApiModel, new()
        {
            var model = new TModel();

            model.Id   = htmlContent.Id;
            model.Body = htmlContent.Body;
            return(model);
        }
            public async Task <AddOrUpdateHTMLContentResponse> Handle(AddOrUpdateHTMLContentRequest request)
            {
                var entity = await _dataContext.HTMLContents
                             .SingleOrDefaultAsync(x => x.Id == request.HTMLContent.Id && x.IsDeleted == false);

                if (entity == null)
                {
                    _dataContext.HTMLContents.Add(entity = new HTMLContent());
                }
                entity.Name = request.HTMLContent.Name;
                await _dataContext.SaveChangesAsync();

                return(new AddOrUpdateHTMLContentResponse()
                {
                });
            }
Exemple #3
0
 public void ParseText(string text, HTMLContent content)
 {
     string[] array = content.GetFlags().Select(f => f.ToString()).ToArray();
     foreach (string flag in array)
     {
         if (HtmlUtils.HTML_REGEXES[flag].Key)
         {
             var keyValues = HtmlUtils.HTML_REGEXES[flag].Value.FromKeyValues(text).Distinct(new KeyValuePairComparer <string, string>());
             if (keyValues.Any())
             {
                 if (Dictionaries == null)
                 {
                     Dictionaries = new Dictionary <string, IEnumerable <KeyValuePair <string, string> > >()
                     {
                         { flag, keyValues }
                     }
                 }
                 ;
                 else if (!Dictionaries.TryAdd(flag, keyValues, out IEnumerable <KeyValuePair <string, string> > existingValues))
                 {
                     Dictionaries[flag] = keyValues.Concat(existingValues).Distinct(new KeyValuePairComparer <string, string>());
                 }
             }
         }
         else
         {
             var values = HtmlUtils.HTML_REGEXES[flag].Value.MatchesValues(text).Distinct();
             if (values.Any())
             {
                 if (Collections == null)
                 {
                     Collections = new Dictionary <string, IEnumerable <string> >()
                     {
                         { flag, values }
                     }
                 }
                 ;
                 else if (!Collections.TryAdd(flag, values, out IEnumerable <string> existingValues))
                 {
                     Collections[flag] = values.Concat(existingValues).Distinct();
                 }
             }
         }
     }
     array = null;
 }
        public ActionResult CreatePost(HTMLContent x)
        {
            var newPost = new Domain.Post();

            newPost.Content = x.HtmlContent;
            newPost.Title = x.Title;
            newPost.IsApproved = true;
            newPost.DatePosted = DateTime.Now;

            if (ModelState.IsValid)
            {
                var manager = new BlogManager();
                var response = manager.AddPost(newPost);

                return RedirectToAction("Index");
            }
            else
            {
                return View("OwnerAddPost");
            }
        }
        // Return DOMElement instead of Tag, since we -could- return
        private DOMElement _ParseTag(int startPosition)
        {
            // Initialize new Tag and empty Attribute
            HTMLTag          tag = new HTMLTag(startPosition);
            HTMLTagAttribute currentAttribute = null;

            // Start looping through the HTML (skip 1 char since we're already at the '<'
            tagParserState = TagParserState.ExpectingTagName;
            int currentPosition = startPosition + 1;

            while (currentPosition < _HTML.Length)
            {
                // Read char and advance
                char chr = _HTML[currentPosition];

                switch (tagParserState)
                {
                    #region TagParserState.ExpectingTagName - Look for an optional '/' and/or a tag name and possibly an ending '>' (if there's a '/' found)

                /*
                 * MATCHES:
                 * <DIV ATTRIBUTE="FOO" ATTR = 'BAR'> or </DIV>
                 *  ‾‾‾                                   ‾‾‾‾‾
                 */

                // When we're start a tag and waiting for the tag name...
                case TagParserState.ExpectingTagName:
                {
                    if (isAlphaNumericChar(chr))
                    {
                        // A letter in the tag name - add it to sbTemp and read the rest of the tag name
                        tag.TagName = _readAlphaNumericWord(currentPosition);

                        if (tag.TagName.StartsWith("!--"))
                        {
                            // HTML comment
                            HTMLContent comment = new HTMLContent(startPosition, _readUntil(startPosition, "-->"));
                            return(comment);
                        }
                        else
                        {
                            // Any tag conversions?
                            switch (tag.TagName.ToLower())
                            {
                            case "form":
                                tag = new HTMLForm(tag.StartPosition)
                                {
                                    TagName = tag.TagName
                                };
                                break;

                            case "input":
                                tag = new HTMLInput(tag.StartPosition)
                                {
                                    TagName = tag.TagName
                                };
                                break;

                            case "select":
                                tag = new HTMLSelect(tag.StartPosition)
                                {
                                    TagName = tag.TagName
                                };
                                break;

                            case "option":
                                tag = new HTMLSelectOption(tag.StartPosition)
                                {
                                    TagName = tag.TagName
                                };
                                break;

                            case "textarea":
                                tag = new HTMLTextarea(tag.StartPosition)
                                {
                                    TagName = tag.TagName
                                };
                                break;
                            }

                            // Advance position by name length
                            currentPosition += tag.TagName.Length;
                            tagParserState   = TagParserState.ExpectingTagContentsOrEnd;
                        }
                    }
                    else if (chr == '/')
                    {
                        // This is a closing tag like </div> - read the tag name and close it
                        tag.IsClosingTag = true;

                        // Advance to the start of the tag name and read it
                        currentPosition  = this._indexOfNextNonWhitespaceChar(currentPosition + 1);
                        tag.TagName      = _readAlphaNumericWord(currentPosition);
                        currentPosition += tag.TagName.Length;

                        // Advance to end of tag '>'
                        currentPosition += _readUntil(currentPosition, '>').Length - 1;
                        tagParserState   = TagParserState.TagEnded;
                    }
                }
                break;
                    #endregion

                    #region TagParserState.ExpectingAttributeNameOrTagEnd - Inside the tag, looking for either alpha chars (start of an attribute), or a '/' self-closing flag, or the closing '>' character
                case TagParserState.ExpectingTagContentsOrEnd:

                    // Advance to the next non-whitespace char
                    currentPosition = _indexOfNextNonWhitespaceChar(currentPosition);
                    chr             = _HTML[currentPosition];

                    if (chr == '/')
                    {
                        /* MATCHES: <IMG />
                         *               ‾‾
                         */

                        // Self-closing tag
                        tag.SelfClosed = true;

                        // Advance to end of tag '>'
                        currentPosition += _readUntil(currentPosition, '>').Length - 1;
                        tagParserState   = TagParserState.TagEnded;
                    }
                    else if (chr == '>')
                    {
                        /* MATCHES: <DIV>
                         *              ‾
                         */

                        // End of tag
                        tagParserState = TagParserState.TagEnded;
                    }
                    else if ((chr == '"') || (chr == '\''))
                    {
                        // Unnamed, quoted attribute value, like a DOCTYPE dtd path <!DOCTYPE html "blah blah">

                        // Read the quoted value
                        string attributeValue = _readValue(currentPosition);

                        // Build a new attribute
                        currentAttribute = new HTMLTagAttribute(currentPosition, null, attributeValue, chr.ToString());

                        // Advance the position
                        currentPosition += attributeValue.Length;

                        // Finish the attribute and clear it
                        currentAttribute.EndPosition = currentPosition;
                        tag.Attributes.Add(currentAttribute);
                        currentAttribute = null;
                    }
                    else if (isAlphaChar(chr))
                    {
                        /*
                         * MATCHES:
                         * <DIV ATTRIBUTE="FOO" ATTR = 'BAR'>
                         *      ‾‾‾‾‾‾‾‾‾       ‾‾‾‾
                         */
                        // A letter in the attribute name - read the rest of the attribute
                        string attributeName = _readAlphaNumericWord(currentPosition);
                        currentAttribute = new HTMLTagAttribute(currentPosition, attributeName);

                        // Advance position to the end of the name
                        currentPosition += attributeName.Length;

                        // Do we have an attribute value?
                        int nextNonWhitespaceChar = _indexOfNextNonWhitespaceChar(currentPosition);
                        if (_HTML[nextNonWhitespaceChar] == '=')
                        {
                            // tagParserState = TagParserState.ExpectingAttributeValue;
                            currentPosition = nextNonWhitespaceChar + 1;

                            // Advance to the next non-whitespace char (in case of space-separated values like 'foo = "bar"'
                            nextNonWhitespaceChar = _indexOfNextNonWhitespaceChar(currentPosition);
                            string rawAttributeValue = _readValue(currentPosition);
                            currentAttribute.Value = rawAttributeValue;

                            // Advance position to end of the value
                            currentPosition += rawAttributeValue.Length;
                        }
                        else
                        {
                            // A standalone attributelike <!DOCTYPE html "foobar">
                            //                                      ‾‾‾‾
                        }

                        // End of attribute - mark the end position and add to the tag
                        currentAttribute.EndPosition = currentPosition;
                        tag.Attributes.Add(currentAttribute);

                        // Reset attribute
                        currentAttribute = null;
                    }
                    break;
                    #endregion
                }

                // End the tag?
                if (tagParserState == TagParserState.TagEnded)
                {
                    // Apply transformations?
                    if (_transforms == Transformations.LowercaseNames)
                    {
                        tag.TagName = tag.TagName.ToLower();
                        foreach (HTMLTagAttribute attr in tag.Attributes)
                        {
                            if (attr.Name != null)
                            {
                                attr.Name = attr.Name.ToLower();
                            }
                        }
                    }
                    else if (_transforms == Transformations.UppercaseNames)
                    {
                        tag.TagName = tag.TagName.ToUpper();
                        foreach (HTMLTagAttribute attr in tag.Attributes)
                        {
                            if (attr.Name != null)
                            {
                                attr.Name = attr.Name.ToUpper();
                            }
                        }
                    }

                    // Remove empty attributes list
                    if (tag.Attributes.Count == 0)
                    {
                        tag.Attributes = null;
                    }

                    // Mark the end position of the tag and return it
                    tag.MarkEndPosition(currentPosition);
                    return(tag);
                }
            }

            // Shouldn't really get here...
            return(tag);
        }
 public static HTMLContentApiModel FromHTMLContent(HTMLContent hTMLContent)
 => FromHTMLContent <HTMLContentApiModel>(hTMLContent);