All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
FunctionCapture.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_FUNCTION_CAPTURE_H_INCLUDED
16 #define HEXT_FUNCTION_CAPTURE_H_INCLUDED
17 
18 /// @file
19 /// Declares hext::FunctionCapture
20 
21 #include "hext/Capture.h"
22 #include "hext/CaptureFunction.h"
23 #include "hext/Cloneable.h"
24 #include "hext/Result.h"
25 #include "hext/StringPipe.h"
26 
27 #include <string>
28 
29 #include <gumbo.h>
30 #include <boost/optional.hpp>
31 
32 
33 namespace hext {
34 
35 
36 /// Captures the result of applying a function to an HTML node.
37 ///
38 /// @par Example:
39 /// ~~~~~~~~~~~~~
40 /// GumboNode * node = ...; // <div>5 reasons why foo is better than bar</div>
41 /// FunctionCapture text(
42 /// TextBuiltin, // predefined CaptureFunction
43 /// "text" // result name
44 /// );
45 /// if( auto result = text.capture(node) )
46 /// assert(
47 /// *result == ResultPair("text", "5 reasons why foo is better than bar")
48 /// );
49 /// ~~~~~~~~~~~~~
50 ///
51 /// @par Example with regex:
52 /// ~~~~~~~~~~~~~~~~~~~~~~~~
53 /// GumboNode * node = ...; // <div>The result is 25cm.</div>
54 /// FunctionCapture centimeters(
55 /// InnerHtmlBuiltin, // predefined CaptureFunction
56 /// "centimeters", // result name
57 /// std::make_unique<RegexPipe>(boost::regex("(\\d+)cm"))
58 /// );
59 /// if( auto result = centimeters.capture(node) )
60 /// assert(*result == ResultPair("centimeters", "25"));
61 /// ~~~~~~~~~~~~~~~~~~~~~~~~
62 class FunctionCapture final : public Cloneable<FunctionCapture, Capture>
63 {
64 public:
65  /// Constructs a FunctionCapture.
66  ///
67  /// @param func: The function that will be applied to an HTML node.
68  /// @param result_name: The name for the result that is returned from
69  /// FuntionCapture::capture.
70  /// @param pipe: If given, the captured value will be sent through
71  /// this StringPipe before returning from capture().
73  std::string result_name,
74  std::unique_ptr<StringPipe> pipe = {}) noexcept;
75 
76  FunctionCapture(const FunctionCapture& other);
77  FunctionCapture(FunctionCapture&&) noexcept = default;
78  FunctionCapture& operator=(const FunctionCapture& other);
79  FunctionCapture& operator=(FunctionCapture&&) noexcept = default;
80 
81  /// Captures the result of calling a given CaptureFunction with node as its
82  /// argument. Optionally applies a StringPipe to the value before returning.
83  ///
84  /// @param node: A pointer to a GumboNode.
85  ///
86  /// @returns A pair in the form of {result_name, result_value} or
87  /// an empty optional if the capture failed.
88  boost::optional<ResultPair> capture(const GumboNode * node) const override;
89 
90 private:
91  /// The function that will be applied to an HTML node.
92  CaptureFunction func_;
93 
94  /// The result name of the captured contents, e.g. ResultPair("text", ...).
95  std::string name_;
96 
97  /// An optional StringPipe that will be applied to the result.
98  std::unique_ptr<StringPipe> pipe_;
99 };
100 
101 
102 } // namespace hext
103 
104 
105 #endif // HEXT_FUNCTION_CAPTURE_H_INCLUDED
106 
std::function< std::string(const GumboNode *)> CaptureFunction
A type of std::function that receives an HTML element and returns a string.
FunctionCapture(CaptureFunction func, std::string result_name, std::unique_ptr< StringPipe > pipe={}) noexcept
Constructs a FunctionCapture.
Captures the result of applying a function to an HTML node.
Declares hext::StringPipe.
Typedefs for results returned from capturing HTML.
Curiously recurring template pattern that extends a base class to provide a virtual method Cloneable:...
Definition: Cloneable.h:34
Declares hext::CaptureFunction.
Defines template hext::Cloneable.
FunctionCapture & operator=(const FunctionCapture &other)
boost::optional< ResultPair > capture(const GumboNode *node) const override
Captures the result of calling a given CaptureFunction with node as its argument. ...
Declares hext::Capture.