All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
AttributeCapture.h
Go to the documentation of this file.
1 // Copyright 2015, 2016 Thomas Trapp
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef HEXT_ATTRIBUTE_CAPTURE_H_INCLUDED
16 #define HEXT_ATTRIBUTE_CAPTURE_H_INCLUDED
17 
18 /// @file
19 /// Declares hext::AttributeCapture
20 
21 #include "hext/Capture.h"
22 #include "hext/Cloneable.h"
23 #include "hext/Result.h"
24 #include "hext/StringPipe.h"
25 
26 #include <string>
27 
28 #include <gumbo.h>
29 #include <boost/optional.hpp>
30 
31 
32 namespace hext {
33 
34 
35 /// Captures an HTML Element's attribute.
36 ///
37 /// @par Example:
38 /// ~~~~~~~~~~~~~
39 /// GumboNode * node = ...; // <img src="bob.jpg"/>
40 /// AttributeCapture img(
41 /// "src", // attribute name
42 /// "image" // result name
43 /// );
44 /// if( auto result = img.capture(node) )
45 /// // attribute found and captured
46 /// assert(*result == ResultPair("image", "bob.jpg"));
47 /// ~~~~~~~~~~~~~
48 ///
49 /// @par Example with regex:
50 /// ~~~~~~~~~~~~~~~~~~~~~~~~
51 /// GumboNode * node = ...; // <a href="/highway-61"></a>
52 /// AttributeCapture highway(
53 /// "href", // attribute name
54 /// "U.S. Route" // result name
55 /// std::make_unique<RegexPipe>(boost::regex("\\d+"))
56 /// );
57 /// if( auto result = highway.capture(node) )
58 /// // attribute found and captured
59 /// assert(*result == ResultPair("U.S. Route", "61"));
60 /// ~~~~~~~~~~~~~~~~~~~~~~~~
61 class AttributeCapture final : public Cloneable<AttributeCapture, Capture>
62 {
63 public:
64  /// Constructs an AttributeCapture.
65  ///
66  /// @param attr_name: The name of the HTML attribute whose value will be
67  /// captured.
68  /// @param result_name: The name for the result that is returned from
69  /// AttributeCapture::capture.
70  /// @param pipe: If given, the captured value will be sent through
71  /// this StringPipe before returning from capture().
72  AttributeCapture(std::string attr_name,
73  std::string result_name,
74  std::unique_ptr<StringPipe> pipe = nullptr) noexcept;
75 
76  AttributeCapture(const AttributeCapture& other);
77  AttributeCapture(AttributeCapture&&) noexcept = default;
79  AttributeCapture& operator=(AttributeCapture&&) noexcept = default;
80 
81  /// Captures an HTML element's attribute called attr_name (as given in the
82  /// constructor). Optionally applies a StringPipe to the value before
83  /// returning.
84  ///
85  /// @param node: A pointer to a GumboNode of type GUMBO_NODE_ELEMENT.
86  ///
87  /// @returns A pair in the form of {result_name, attribute_value} or
88  /// an empty optional if the attribute cannot be found.
89  boost::optional<ResultPair> capture(const GumboNode * node) const override;
90 
91 private:
92  /// The name of the HTML attribute whose value will be captured.
93  std::string attr_name_;
94 
95  /// The result name of the captured contents, e.g. ResultPair("href", ...).
96  std::string name_;
97 
98  /// An optional StringPipe that will be applied to the result.
99  std::unique_ptr<StringPipe> pipe_;
100 };
101 
102 
103 } // namespace hext
104 
105 
106 #endif // HEXT_ATTRIBUTE_CAPTURE_H_INCLUDED
107 
boost::optional< ResultPair > capture(const GumboNode *node) const override
Captures an HTML element's attribute called attr_name (as given in the constructor).
Declares hext::StringPipe.
Typedefs for results returned from capturing HTML.
Captures an HTML Element's attribute.
Curiously recurring template pattern that extends a base class to provide a virtual method Cloneable:...
Definition: Cloneable.h:34
Defines template hext::Cloneable.
AttributeCapture(std::string attr_name, std::string result_name, std::unique_ptr< StringPipe > pipe=nullptr) noexcept
Constructs an AttributeCapture.
AttributeCapture & operator=(const AttributeCapture &other)
Declares hext::Capture.