html2md  v1.6.6
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 if (tableData.empty()) {
71 return "";
72 }
73
74 // Determine maximum width of each column
75 vector<size_t> columnWidths(tableData[0].size(), 0);
76 for (const auto &row : tableData) {
77 if (columnWidths.size() < row.size()) {
78 columnWidths.resize(row.size(), 0);
79 }
80
81 for (size_t i = 0; i < row.size(); ++i) {
82 columnWidths[i] = std::max(columnWidths[i], row[i].size());
83 }
84 }
85
86 // Build the formatted table
87 std::ostringstream formattedTable;
88 for (size_t rowNumber = 0; rowNumber < tableData.size(); ++rowNumber) {
89 const auto &row = tableData[rowNumber];
90
91 formattedTable << "|";
92
93 for (size_t i = 0; i < row.size(); ++i) {
94 if (rowNumber == 1) {
95 formattedTable << enlargeTableHeaderLine(row[i], columnWidths[i] + 2)
96 << "|";
97 continue;
98 }
99 formattedTable << " " << std::setw(columnWidths[i]) << std::left << row[i]
100 << " |";
101 }
102 formattedTable << "\n";
103 }
104
105 return formattedTable.str();
106}
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