Skip to content

Commit

Permalink
Fix merging issues with version 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
marimeireles committed Apr 22, 2020
2 parents e152ecb + ff9dc4c commit ce77eb9
Show file tree
Hide file tree
Showing 21 changed files with 822 additions and 74 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ project(tabulate VERSION 1.0.0 LANGUAGES CXX)
option(tabulate_BUILD_TESTS OFF)
option(SAMPLES "Build Samples" OFF)

if(MSVC)
if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]")
string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4")
endif()
elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -pedantic")
endif()

include(CMakePackageConfigHelpers)
include(GNUInstallDirs)

Expand Down
132 changes: 128 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<a href="https://github.com/p-ranav/tabulate/blob/master/LICENSE">
<img src="https://img.shields.io/badge/License-MIT-yellow.svg" alt="license"/>
</a>
<img src="https://img.shields.io/badge/version-1.0-blue.svg?cacheSeconds=2592000" alt="version"/>
<img src="https://img.shields.io/badge/version-1.1-blue.svg?cacheSeconds=2592000" alt="version"/>
</p>

<p align="center">
Expand All @@ -38,6 +38,9 @@
* [Range-based Iteration](#range-based-iteration)
* [Nested Tables](#nested-tables)
* [UTF-8 Support](#utf-8-support)
* [Exporters](#exporters)
* [Markdown](#markdown)
* [AsciiDoc](#asciidoc)
* [Building Samples](#building-samples)
* [Contributing](#contributing)
* [License](#license)
Expand Down Expand Up @@ -80,7 +83,7 @@ You can format this table using `Table.format()` which returns a `Format` object

You can access rows in the table using `Table[row_index]`. This will return a `Row` object on which you can similarly call `Row.format()` to format properties of all the cells in that row.

Now, let's format the header of the table. The following code changes the font background of the header row to `red`, aligns the cell contents to `center` and applied a padding to the top and bottom of the row.
Now, let's format the header of the table. The following code changes the font background of the header row to `red`, aligns the cell contents to `center` and applies a padding to the top and bottom of the row.

```cpp
universal_constants[0].format()
Expand Down Expand Up @@ -137,7 +140,7 @@ This enables overriding the formatting for a particular cell even though row or

`tabulate` supports automatic word-wrapping when printing cells.

Although word-wrapping is automatic, there is a simple override. Automatic word-wrapping is used only if the cell contents do not have any embedded newline `\n` characters. So, you can embed newline characters in the cell contents and enfore the word-wrapping manually.
Although word-wrapping is automatic, there is a simple override. Automatic word-wrapping is used only if the cell contents do not have any embedded newline `\n` characters. So, you can embed newline characters in the cell contents and enforce the word-wrapping manually.

```cpp
#include <tabulate/table.hpp>
Expand All @@ -160,7 +163,7 @@ int main() {

* The above table has 1 row and 2 columns.
* The first cell has automatic word-wrapping.
* The second cell uses the embedded newline characters in the cell contents - even though the second column has plenty of space (50 characters width), it uses user-provided newline characters to break into new lines and enfore the cell style.
* The second cell uses the embedded newline characters in the cell contents - even though the second column has plenty of space (50 characters width), it uses user-provided newline characters to break into new lines and enforce the cell style.
* **NOTE**: Whether word-wrapping is automatic or not, `tabulate` performs a trim operation on each line of each cell to remove whitespace characters from either side of line.

<p align="center">
Expand Down Expand Up @@ -592,6 +595,127 @@ You can explicitly set the locale for a cell using `.format().locale(value)`. No
table[10][1].format().locale("he_IL.UTF-8"); // Hebrew
```

## Exporters

### Markdown

Tables can be exported to GitHub-flavored markdown using a `MarkdownExporter`. Simply create an exporter object and call `exporter.dump(table)` to generate a Markdown-formatted `std::string`.

```cpp
#include <tabulate/markdown_exporter.hpp>
using namespace tabulate;

int main() {
Table movies;
movies.add_row({"S/N", "Movie Name", "Director", "Estimated Budget", "Release Date"});
movies.add_row({"tt1979376", "Toy Story 4", "Josh Cooley", "$200,000,000", "21 June 2019"});
movies.add_row({"tt3263904", "Sully", "Clint Eastwood", "$60,000,000", "9 September 2016"});
movies.add_row(
{"tt1535109", "Captain Phillips", "Paul Greengrass", "$55,000,000", " 11 October 2013"});

// center align 'Director' column
movies.column(2).format().font_align(FontAlign::center);

// right align 'Estimated Budget' column
movies.column(3).format().font_align(FontAlign::right);

// right align 'Release Date' column
movies.column(4).format().font_align(FontAlign::right);

// Color header cells
for (size_t i = 0; i < 5; ++i) {
movies[0][i].format().font_color(Color::yellow).font_style({FontStyle::bold});
}

// Export to Markdown
MarkdownExporter exporter;
auto markdown = exporter.dump(movies);

// tabulate::table
std::cout << movies << "\n\n";

// Exported Markdown
std::cout << markdown << std::endl;
}
```

<p align="center">
<img src="img/markdown_export.png"/>
</p>

The above table renders in Markdown like below.

**NOTE**: Unlike `tabulate`, you cannot align individual cells in Markdown. Alignment is on a per-column basis. Markdown allows a second header row where such column-wise alignment can be specified. The `MarkdownExporter` uses the formatting of the header cells in the original `tabulate::Table` to decide how to align each column. As per the Markdown spec, columns are left-aligned by default.

| S/N | Movie Name | Director | Estimated Budget | Release Date |
| :---- | :---- | :---: | ----: | ----: |
| tt1979376 | Toy Story 4 | Josh Cooley | $200,000,000 | 21 June 2019 |
| tt3263904 | Sully | Clint Eastwood | $60,000,000 | 9 September 2016 |
| tt1535109 | Captain Phillips | Paul Greengrass | $55,000,000 | 11 October 2013 |

### AsciiDoc

Tabulate can export tables as AsciiDoc using an `AsciiDocExporter`.

```cpp
#include <tabulate/asciidoc_exporter.hpp>
using namespace tabulate;

int main() {
Table movies;
movies.add_row({"S/N", "Movie Name", "Director", "Estimated Budget", "Release Date"});
movies.add_row({"tt1979376", "Toy Story 4", "Josh Cooley", "$200,000,000", "21 June 2019"});
movies.add_row({"tt3263904", "Sully", "Clint Eastwood", "$60,000,000", "9 September 2016"});
movies.add_row(
{"tt1535109", "Captain Phillips", "Paul Greengrass", "$55,000,000", " 11 October 2013"});

// center align 'Director' column
movies.column(2).format().font_align(FontAlign::center);

// right align 'Estimated Budget' column
movies.column(3).format().font_align(FontAlign::right);

// right align 'Release Date' column
movies.column(4).format().font_align(FontAlign::right);

movies[1][2].format().font_style({FontStyle::bold, FontStyle::italic});
movies[2][1].format().font_style({FontStyle::italic});

// Color header cells
for (size_t i = 0; i < 5; ++i) {
movies[0][i]
.format()
.font_color(Color::white)
.font_style({FontStyle::bold})
.background_color(Color::blue);
}

AsciiDocExporter exporter;
auto asciidoc = exporter.dump(movies);

// tabulate::table
std::cout << movies << "\n\n";

// Exported AsciiDoc
std::cout << asciidoc << std::endl;
}
```
Below is the export of the example above:

```
[cols="<,<,^,>,>"]
|===
|*S/N*|*Movie Name*|*Director*|*Estimated Budget*|*Release Date*
|tt1979376|Toy Story 4|*_Josh Cooley_*|$200,000,000|21 June 2019
|tt3263904|_Sully_|Clint Eastwood|$60,000,000|9 September 2016
|tt1535109|Captain Phillips|Paul Greengrass|$55,000,000| 11 October 2013
|===
```
The rendered output you can see here: http://tpcg.io/pbbfU3ks

**NOTE** Alignment is only supported per column. The font styles `FontStyle::bold` and `FontStyle::italic` can be used for each cell, also in combination.

## Building Samples

There are a number of samples in the `samples/` directory, e.g., [Mario](https://github.com/p-ranav/tabulate/blob/master/samples/mario.cpp). You can build these samples by running the following commands.
Expand Down
Binary file added img/markdown_export.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
137 changes: 137 additions & 0 deletions include/tabulate/asciidoc_exporter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
__ ___. .__ __
_/ |______ \_ |__ __ __| | _____ _/ |_ ____
\ __\__ \ | __ \| | \ | \__ \\ __\/ __ \
| | / __ \| \_\ \ | / |__/ __ \| | \ ___/
|__| (____ /___ /____/|____(____ /__| \___ >
\/ \/ \/ \/
Table Maker for Modern C++
https://github.com/p-ranav/tabulate
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
SPDX-License-Identifier: MIT
Copyright (c) 2019 Pranav Srinivas Kumar <pranav.srinivas.kumar@gmail.com>.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
#include <algorithm>
#include <optional>
#include <sstream>
#include <string>
#include <tabulate/exporter.hpp>

namespace tabulate {

class AsciiDocExporter : public Exporter {

static const char new_line = '\n';

public:
std::string dump(Table &table) override {
std::stringstream ss;
ss << add_alignment_header(table);
ss << new_line;

const auto rows = table.rows_;
// iterate content and put text into the table.
for (size_t row_index = 0; row_index < rows; row_index++) {
auto &row = table[row_index];

for (size_t cell_index = 0; cell_index < row.size(); cell_index++) {
ss << "|";
ss << add_formatted_cell(row[cell_index]);
}
ss << new_line;
if (row_index == 0) {
ss << new_line;
}
}

ss << "|===";
return ss.str();
}

private:
std::string add_formatted_cell(Cell &cell) const {
std::stringstream ss;
auto format = cell.format();
std::string cell_string = cell.get_text();

auto font_style = format.font_style_.value();

bool format_bold = false;
bool format_italic = false;
std::for_each(font_style.begin(), font_style.end(), [&](auto &style) {
if (style == FontStyle::bold) {
format_bold = true;
} else if (style == FontStyle::italic) {
format_italic = true;
}
});

if (format_bold) {
ss << '*';
}
if (format_italic) {
ss << '_';
}

ss << cell_string;
if (format_italic) {
ss << '_';
}
if (format_bold) {
ss << '*';
}
return ss.str();
}

std::string add_alignment_header(Table &table) {
std::stringstream ss;
ss << (R"([cols=")");

size_t column_count = table[0].size();
size_t column_index = 0;
for (auto &cell : table[0]) {
auto format = cell.format();

if (format.font_align_.value() == FontAlign::left) {
ss << '<';
} else if (format.font_align_.value() == FontAlign::center) {
ss << '^';
} else if (format.font_align_.value() == FontAlign::right) {
ss << '>';
}

++column_index;
if (column_index != column_count) {
ss << ",";
}
}

ss << R"("])";
ss << new_line;
ss << "|===";

return ss.str();
}
};

} // namespace tabulate

0 comments on commit ce77eb9

Please sign in to comment.