All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
AttributeCapture.h
Go to the documentation of this file.
1 // Copyright 2015-2017 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 #include "hext/Visibility.h"
26 
27 #include <optional>
28 #include <string>
29 
30 #include <gumbo.h>
31 
32 
33 namespace hext {
34 
35 
36 /// Captures an HTML Element's attribute.
37 ///
38 /// @par Example:
39 /// ~~~~~~~~~~~~~
40 /// GumboNode * node = ...; // <img src="bob.jpg"/>
41 /// AttributeCapture img(
42 /// "src", // attribute name
43 /// "image" // result name
44 /// );
45 /// if( auto result = img.capture(node) )
46 /// // attribute found and captured
47 /// assert(*result == ResultPair("image", "bob.jpg"));
48 /// ~~~~~~~~~~~~~
49 ///
50 /// @par Example with regex:
51 /// ~~~~~~~~~~~~~~~~~~~~~~~~
52 /// GumboNode * node = ...; // <a href="/highway-61"></a>
53 /// AttributeCapture highway(
54 /// "href", // attribute name
55 /// "U.S. Route" // result name
56 /// std::make_unique<RegexPipe>(boost::regex("\\d+"))
57 /// );
58 /// if( auto result = highway.capture(node) )
59 /// // attribute found and captured
60 /// assert(*result == ResultPair("U.S. Route", "61"));
61 /// ~~~~~~~~~~~~~~~~~~~~~~~~
63  : public Cloneable<AttributeCapture, Capture>
64 {
65 public:
66  /// Constructs an AttributeCapture.
67  ///
68  /// @param attr_name: The name of the HTML attribute whose value will be
69  /// captured.
70  /// @param result_name: The name for the result that is returned from
71  /// AttributeCapture::capture.
72  /// @param pipe: If given, the captured value will be sent through
73  /// this StringPipe before returning from capture().
74  AttributeCapture(std::string attr_name,
75  std::string result_name,
76  std::unique_ptr<StringPipe> pipe = nullptr) noexcept;
77 
78  AttributeCapture(const AttributeCapture& other);
79  AttributeCapture(AttributeCapture&&) noexcept = default;
80  AttributeCapture& operator=(const AttributeCapture& other);
81  AttributeCapture& operator=(AttributeCapture&&) noexcept = default;
82 
83  /// Captures an HTML element's attribute called attr_name (as given in the
84  /// constructor). Optionally applies a StringPipe to the value before
85  /// returning.
86  ///
87  /// @param node: A pointer to a GumboNode of type GUMBO_NODE_ELEMENT.
88  ///
89  /// @returns A pair in the form of {result_name, attribute_value} or
90  /// an empty optional if the attribute cannot be found.
91  std::optional<ResultPair> capture(const GumboNode * node) const override;
92 
93 private:
94  /// The name of the HTML attribute whose value will be captured.
95  std::string attr_name_;
96 
97  /// The result name of the captured contents, e.g. ResultPair("href", ...).
98  std::string name_;
99 
100  /// An optional StringPipe that will be applied to the result.
101  std::unique_ptr<StringPipe> pipe_;
102 };
103 
104 
105 } // namespace hext
106 
107 
108 #endif // HEXT_ATTRIBUTE_CAPTURE_H_INCLUDED
109 
Declares hext::StringPipe.
Defines HEXT_PUBLIC and HEXT_PRIVATE.
Typedefs for results returned from capturing HTML.
Captures an HTML Element's attribute.
#define HEXT_PUBLIC
Definition: Visibility.h:26
Curiously recurring template pattern that extends a base class to provide a virtual method Cloneable:...
Definition: Cloneable.h:36
Defines template hext::Cloneable.
Declares hext::Capture.