-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Another utility. I needed to extract all the internal links from the …
…source files. ExtractLinks was born.
- Loading branch information
1 parent
70bd088
commit 66c74fa
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
@echo off | ||
@REM Because the shell is brain dead, or appears so, I need | ||
@REM to pass each file individually. It cannot cope with a | ||
@REM wildcard list of files such as Keywords*.clean.rst. Sigh. | ||
@REM | ||
for %%f in (*.clean.rst) do @extractLinks %%f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include <iostream> | ||
#include <fstream> | ||
#include <iomanip> | ||
#include <string> | ||
#include <regex> | ||
|
||
// | ||
// extractLinks <list_of_files> | ||
// | ||
// This program will read the list of files and for each one found, | ||
// will extract all the links. | ||
// | ||
// Links are found as follows, in the source files: | ||
// | ||
// `Link Text <URL>`__ | ||
// | ||
|
||
using std::string; | ||
using std::getline; | ||
using std::cin; | ||
using std::cout; | ||
using std::endl; | ||
using std::ifstream; | ||
using std::regex; | ||
using std::regex_match; | ||
|
||
|
||
// Globals. Yes, I know... | ||
string this_line; | ||
|
||
// Function prototype(s). | ||
void doFile(const char *fname); | ||
|
||
|
||
int main (int argc, char *argv[]) | ||
{ | ||
for (int x = 1; x < argc; x++) { | ||
doFile(argv[x]); | ||
} | ||
return 0; | ||
} | ||
|
||
|
||
void doFile(const char *fname) | ||
{ | ||
// Open a file, read it, extract the keywords. | ||
|
||
unsigned lineNumber = 0; | ||
string linkText; | ||
regex linkRegex(".*`.*<.*>`__.*"); // This should match a valid link. | ||
|
||
ifstream ifs (fname, std::ifstream::in); | ||
while (ifs.good()) { | ||
getline(ifs, this_line); | ||
lineNumber++; | ||
|
||
// There can be more than one link on a line of course! | ||
while (regex_match(this_line, linkRegex)) { | ||
|
||
// Where are we? For debugging. | ||
//cout << endl << fname << ": " << lineNumber << ":" << endl; | ||
//cout << this_line << endl; | ||
|
||
// Extract the link in this line. | ||
size_t posLink = this_line.find("`", 0, 1); | ||
size_t posEndLink = this_line.find("`_", posLink + 1, 2); | ||
|
||
// Write out the link details. | ||
linkText = this_line.substr(posLink, posEndLink - posLink + 3); | ||
cout << linkText << " in: " << fname << " at line: " << lineNumber << endl; | ||
|
||
// Adjust the line. | ||
this_line = this_line.substr(posEndLink+3, string::npos); | ||
} | ||
} | ||
|
||
ifs.close(); | ||
} | ||
|