html2md  v1.6.0
Simple and fast HTML to Markdown converter
Loading...
Searching...
No Matches
table.cpp
Go to the documentation of this file.
1// Copyright (c) Tim Gromeyer
2// Licensed under the MIT License - https://opensource.org/licenses/MIT
3
4#include "table.h"
5
6#include <iomanip>
7#include <iostream>
8#include <sstream>
9#include <vector>
10
11using std::string;
12using std::vector;
13
14const size_t MIN_LINE_LENGTH = 3; // Minimum length of line
15
16void removeLeadingTrailingSpaces(string &str) {
17 size_t firstNonSpace = str.find_first_not_of(' ');
18 if (firstNonSpace == string::npos) {
19 str.clear(); // Entire string is spaces
20 return;
21 }
22
23 size_t lastNonSpace = str.find_last_not_of(' ');
24 str = str.substr(firstNonSpace, lastNonSpace - firstNonSpace + 1);
25}
26
27string enlargeTableHeaderLine(const string &str, size_t length) {
28 if (str.empty() || length < MIN_LINE_LENGTH)
29 return "";
30
31 size_t first = str.find_first_of(':');
32 size_t last = str.find_last_of(':');
33
34 if (first == 0 && first == last)
35 last = string::npos;
36
37 string line = string(length, '-');
38
39 if (first == 0)
40 line[0] = ':';
41 if (last == str.length() - 1)
42 line[length - 1] = ':';
43
44 return line;
45}
46
47string formatMarkdownTable(const string &inputTable) {
48 std::istringstream iss(inputTable);
49 string line;
50 vector<vector<string>> tableData;
51
52 // Parse the input table into a 2D vector
53 while (std::getline(iss, line)) {
54 std::istringstream lineStream(line);
55 string cell;
56 vector<string> rowData;
57
58 while (std::getline(lineStream, cell, '|')) {
59 if (!cell.empty()) {
60 removeLeadingTrailingSpaces(cell); // Use the trim function
61 rowData.push_back(cell);
62 }
63 }
64
65 if (!rowData.empty()) {
66 tableData.push_back(std::move(rowData)); // Move rowData to avoid copying
67 }
68 }
69
70 // Determine maximum width of each column
71 vector<size_t> columnWidths(tableData[0].size(), 0);
72 for (const auto &row : tableData) {
73 if (columnWidths.size() < row.size()) {
74 columnWidths.resize(row.size(), 0);
75 }
76
77 for (size_t i = 0; i < row.size(); ++i) {
78 columnWidths[i] = std::max(columnWidths[i], row[i].size());
79 }
80 }
81
82 // Build the formatted table
83 std::ostringstream formattedTable;
84 for (size_t rowNumber = 0; rowNumber < tableData.size(); ++rowNumber) {
85 const auto &row = tableData[rowNumber];
86
87 formattedTable << "|";
88
89 for (size_t i = 0; i < row.size(); ++i) {
90 if (rowNumber == 1) {
91 formattedTable << enlargeTableHeaderLine(row[i], columnWidths[i] + 2)
92 << "|";
93 continue;
94 }
95 formattedTable << " " << std::setw(columnWidths[i]) << std::left << row[i]
96 << " |";
97 }
98 formattedTable << "\n";
99 }
100
101 return formattedTable.str();
102}
void removeLeadingTrailingSpaces(string &str)
Definition table.cpp:16
string enlargeTableHeaderLine(const string &str, size_t length)
Definition table.cpp:27
string formatMarkdownTable(const string &inputTable)
Definition table.cpp:47
const size_t MIN_LINE_LENGTH
Definition table.cpp:14