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