Skip to content

Commit 0693ce5

Browse files
committed
Complete implementation of retrieval of manifest-specified icons
- Incorporate the "header-only" implementation of the Boost.JSON library and the Parser class from the `lwnws` NWS weather retrieval application for use in parsing retrieved manifest JSON.
1 parent d5ee9ab commit 0693ce5

File tree

7 files changed

+197
-23
lines changed

7 files changed

+197
-23
lines changed

Makefile.am

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,26 @@ AUTOMAKE_OPTIONS = subdir-objects
33
bin_PROGRAMS = ficonic
44

55
ficonic_SOURCES = \
6+
src/program_info.h \
67
src/Curler.cpp \
78
src/Curler.h \
89
src/main.cpp \
910
src/ficon.cpp \
1011
src/ficon.hpp \
11-
src/program_info.h \
12+
src/Parser.cpp \
13+
src/Parser.hpp \
1214
src/IconsRetriever.cpp \
1315
src/IconsRetriever.hpp \
16+
src/HtmlTagAccessor.cpp \
17+
src/HtmlTagAccessor.hpp \
1418
src/RootIconsRetriever.cpp \
1519
src/RootIconsRetriever.hpp \
1620
src/LinkIconsRetriever.cpp \
1721
src/LinkIconsRetriever.hpp \
1822
src/MetaIconsRetriever.cpp \
1923
src/MetaIconsRetriever.hpp \
20-
src/HtmlTagAccessor.cpp \
21-
src/HtmlTagAccessor.hpp
22-
24+
src/PwaIconsRetriever.cpp \
25+
src/PwaIconsRetriever.hpp
2326

2427
ficonic_CPPFLAGS = \
2528
-I/usr/include/ImageMagick-6 \

Makefile.in

Lines changed: 61 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Parser.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Parser.cpp
3+
* - Class Parser method implementation
4+
*
5+
* Copyright 2022 David Yockey
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
#include "Parser.hpp"
22+
23+
///// public /////////////////////////////////////////////////////////
24+
25+
boost::json::value Parser::parse(std::string json) {
26+
jsonParser.reset();
27+
jsonParser.write(json);
28+
return jsonParser.release();
29+
}
30+
31+

src/Parser.hpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Parser.hpp
3+
* - Wrapper class for boost::json::parser
4+
*
5+
* Copyright 2022 David Yockey
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
#ifndef SRC_PARSER_HPP_
22+
#define SRC_PARSER_HPP_
23+
24+
#include <string>
25+
#include <boost/json.hpp>
26+
27+
namespace bjs = boost::json;
28+
29+
class Parser {
30+
private:
31+
bjs::parser jsonParser;
32+
33+
public:
34+
bjs::value parse(std::string json);
35+
};
36+
37+
#endif /* SRC_PARSER_HPP_ */

src/PwaIconsRetriever.cpp

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,59 @@
2929
*/
3030

3131
#include "PwaIconsRetriever.hpp"
32+
#include <boost/json/src.hpp>
33+
34+
namespace bjs = boost::json;
3235

3336
///// private ////////////////////////////////////////////////////////
3437

38+
39+
void PwaIconsRetriever::pullManifestIcons(string manifest, string rel, ficonvector& ficons) {
40+
41+
bjs::value parsed_manifest = parser.parse(manifest);
42+
bjs::array icons( parsed_manifest.at("icons").as_array() );
43+
string imgurl;
44+
45+
for (auto icon : icons) {
46+
imgurl = icon.at("src").as_string();
47+
48+
if ( noHttpProtocol(imgurl) ) {
49+
imgurl = pulledsite_url + clipLeadingSlash(imgurl);
50+
}
51+
52+
pullIcon(imgurl, rel, ficons);
53+
}
54+
}
55+
3556
void PwaIconsRetriever::procIconTag(nodeItr itr, ficonvector& ficons) {
36-
// TODO: Implement procIconTag method
57+
itr->parseAttributes();
58+
string rel = getAttrText(itr, "rel");
59+
60+
if (rel == this->rel) {
61+
string manifest_url = getAttrText(itr, "href");
62+
63+
if ( noHttpProtocol(manifest_url) ) {
64+
manifest_url = pulledsite_url + clipLeadingSlash(manifest_url);
65+
}
66+
67+
string manifest;
68+
Curler::pull(manifest_url, manifest);
69+
pullManifestIcons(manifest, rel, ficons);
70+
}
3771
}
3872

3973
///// public /////////////////////////////////////////////////////////
4074

41-
PwaIconsRetriever::PwaIconsRetriever() : HtmlTagAccessor() {
75+
PwaIconsRetriever::PwaIconsRetriever() : HtmlTagAccessor(), parser() {
4276
}
4377

4478
PwaIconsRetriever::~PwaIconsRetriever() {
4579
}
4680

4781
void PwaIconsRetriever::pull(string url, ficonic::ficonvector& ficons) {
48-
// TODO: Implement pull method
82+
HtmlTagAccessor::pull(url, html);
83+
84+
pulledsite_url = Curler::effective_url();
85+
86+
getIconTags("link", ficons);
4987
}

0 commit comments

Comments
 (0)