Skip to content

Commit 2a3c92f

Browse files
authored
Merge pull request CloudBotIRC#110 from linuxdaemon/gonzobot+markdown-util
Move markdown table generator to cloudbot.util
2 parents 168214a + 5d6cd75 commit 2a3c92f

3 files changed

Lines changed: 20 additions & 29 deletions

File tree

cloudbot/util/formatting.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
4545
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
4646
"""
47-
47+
import copy
4848
import re
4949
import html.entities
5050

@@ -318,3 +318,20 @@ def get_text_list(list_, last_word='or'):
318318
# Translators: This string is used as a separator between list elements
319319
', '.join([i for i in list_][:-1]),
320320
last_word, list_[-1])
321+
322+
323+
def gen_markdown_table(headers, rows):
324+
"""
325+
Generates a Markdown formatted table from the data
326+
"""
327+
rows = copy.copy(rows)
328+
rows.insert(0, headers)
329+
rotated = zip(*reversed(rows))
330+
331+
sizes = tuple(map(lambda l: max(max(map(len, l)), 3), rotated))
332+
rows.insert(1, tuple(('-' * size) for size in sizes))
333+
lines = [
334+
"| {} |".format(' | '.join(cell.ljust(sizes[i]) for i, cell in enumerate(row)))
335+
for row in rows
336+
]
337+
return '\n'.join(lines)

plugins/core/optout.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from cloudbot import hook
1414
from cloudbot.hook import Priority
1515
from cloudbot.util import database, web
16+
from cloudbot.util.formatting import gen_markdown_table
1617

1718
optout_table = Table(
1819
'optout',
@@ -75,20 +76,6 @@ def get_channel_optouts(conn_name, chan=None):
7576
return [opt for opt in optout_cache[conn_name] if not chan or opt.match_chan(chan)]
7677

7778

78-
def gen_markdown_table(headers, rows):
79-
rows = copy.copy(rows)
80-
rows.insert(0, headers)
81-
rotated = zip(*reversed(rows))
82-
83-
sizes = tuple(map(lambda l: max(map(len, l)), rotated))
84-
rows.insert(1, tuple(('-' * size) for size in sizes))
85-
lines = [
86-
"| {} |".format(' | '.join(cell.ljust(sizes[i]) for i, cell in enumerate(row)))
87-
for row in rows
88-
]
89-
return '\n'.join(lines)
90-
91-
9279
def format_optout_list(opts):
9380
headers = ("Channel Pattern", "Hook Pattern", "Allowed")
9481
table = [(opt.channel, opt.hook, "true" if opt.allow else "false") for opt in opts]

plugins/core/plugin_control.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,7 @@
44

55
from cloudbot import hook
66
from cloudbot.util import web
7-
8-
9-
def gen_markdown_table(headers, rows):
10-
rows = list(rows)
11-
rows.insert(0, headers)
12-
rotated = zip(*reversed(rows))
13-
14-
sizes = tuple(map(lambda l: max(map(len, l)), rotated))
15-
rows.insert(1, tuple(('-' * size) for size in sizes))
16-
lines = [
17-
"| {} |".format(' | '.join(cell.ljust(sizes[i]) for i, cell in enumerate(row)))
18-
for row in rows
19-
]
20-
return '\n'.join(lines)
7+
from cloudbot.util.formatting import gen_markdown_table
218

229

2310
@hook.command(permissions=["botcontrol"], autohelp=False)

0 commit comments

Comments
 (0)