From a5f5d6ef235409f9757eef4dbc39d210af59d5a2 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sat, 16 Mar 2019 17:17:25 +0100 Subject: [PATCH] Minor doc updates, fixes and improvements In particular, removed code which no longer worked, made light theme easier on the eyes, added slight syntax highlightning, and fixed search for exact matches. --- .../extra/examples/telegram-client.rst | 27 +++++++------------ .../data/html/css/docs.light.css | 20 +++++++------- telethon_generator/data/html/js/search.js | 4 +-- telethon_generator/docswriter.py | 1 - telethon_generator/generators/docs.py | 24 ++++++++--------- telethon_generator/parsers/tlobject/tlarg.py | 15 +++++++++++ 6 files changed, 48 insertions(+), 43 deletions(-) diff --git a/readthedocs/extra/examples/telegram-client.rst b/readthedocs/extra/examples/telegram-client.rst index f110df1d..afc62ae2 100644 --- a/readthedocs/extra/examples/telegram-client.rst +++ b/readthedocs/extra/examples/telegram-client.rst @@ -447,34 +447,25 @@ However, a day is often more than enough. Sending Messages with Buttons ***************************** -You must sign in as a bot in order to add inline buttons (or normal +**You must sign in as a bot** in order to add inline buttons (or normal keyboards) to your messages. Once you have signed in as a bot specify the `Button ` or buttons to use: .. code-block:: python + from telethon import events from telethon.tl.custom import Button - async def callback(event): - await event.edit('Thank you!') - - client.send_message(chat, 'Hello!', - buttons=Button.inline('Click me', callback)) - - -You can also add the event handler yourself, or change the data payload: - -.. code-block:: python - - from telethon import events - @client.on(events.CallbackQuery) - async def handler(event): - await event.answer('You clicked {}!'.format(event.data)) + async def callback(event): + await event.edit('Thank you for clicking {}!'.format(event.data)) - client.send_message(chat, 'Pick one', buttons=[ + client.send_message(chat, 'A single button, with "clk1" as data', + buttons=Button.inline('Click me', b'clk1')) + + client.send_message(chat, 'Pick one from this grid', buttons=[ [Button.inline('Left'), Button.inline('Right')], - [Button.url('Check my site!', 'https://lonamiwebs.github.io')] + [Button.url('Check this site!', 'https://lonamiwebs.github.io')] ]) You can also use normal buttons (not inline) to request the user's diff --git a/telethon_generator/data/html/css/docs.light.css b/telethon_generator/data/html/css/docs.light.css index 932d1c51..2d0e95d7 100644 --- a/telethon_generator/data/html/css/docs.light.css +++ b/telethon_generator/data/html/css/docs.light.css @@ -1,12 +1,12 @@ body { font-family: 'Nunito', sans-serif; color: #333; - background-color:#fff; + background-color:#eee; font-size: 16px; } a { - color: #42aaed; + color: #329add; text-decoration: none; } @@ -14,7 +14,7 @@ pre { font-family: 'Source Code Pro', monospace; padding: 8px; color: #567; - background: #f0f4f8; + background: #e0e4e8; border-radius: 0; overflow-x: auto; } @@ -30,14 +30,14 @@ table { } table td { - border-top: 1px solid #eee; + border-top: 1px solid #ddd; padding: 8px; } .horizontal { margin-bottom: 16px; list-style: none; - background: #f0f4f8; + background: #e0e4e8; border-radius: 4px; padding: 8px 16px; } @@ -118,14 +118,14 @@ button { font-size: 16px; padding: 8px; color: #000; - background-color: #fff; - border: 2px solid #42aaed; + background-color: #f7f7f7; + border: 2px solid #329add; transition-duration: 300ms; } button:hover { - background-color: #42aaed; - color: #fff; + background-color: #329add; + color: #f7f7f7; } /* https://www.w3schools.com/css/css_navbar.asp */ @@ -143,7 +143,7 @@ ul.together li { ul.together li a { display: block; border-radius: 8px; - background: #f0f4f8; + background: #e0e4e8; padding: 4px 8px; margin: 8px; } diff --git a/telethon_generator/data/html/js/search.js b/telethon_generator/data/html/js/search.js index 909325f7..cb739c07 100644 --- a/telethon_generator/data/html/js/search.js +++ b/telethon_generator/data/html/js/search.js @@ -85,8 +85,8 @@ if (typeof prependPath !== 'undefined') { // Returns the penalty for finding the needle in the haystack // or -1 if the needle wasn't found at all. function find(haystack, needle) { - if (needle.length == 0) { - return true; + if (haystack.indexOf(needle) != -1) { + return 0; } var hi = 0; var ni = 0; diff --git a/telethon_generator/docswriter.py b/telethon_generator/docswriter.py index c3945b28..16aff292 100644 --- a/telethon_generator/docswriter.py +++ b/telethon_generator/docswriter.py @@ -43,7 +43,6 @@ class DocsWriter: """Writes the head part for the generated document, with the given title and CSS """ - # self.title = title self.write( ''' diff --git a/telethon_generator/generators/docs.py b/telethon_generator/generators/docs.py index 77fd05c9..351ca496 100755 --- a/telethon_generator/generators/docs.py +++ b/telethon_generator/generators/docs.py @@ -105,13 +105,14 @@ def _generate_index(root, folder, paths, filename = folder / (BOT_INDEX if bots_index else INDEX) with DocsWriter(root, filename, _get_path_for_type) as docs: # Title should be the current folder name - docs.write_head(str(folder).title(), + docs.write_head(str(folder).replace(os.path.sep, '/').title(), css_path=paths['css'], default_css=paths['default_css']) docs.set_menu_separator(paths['arrow']) _build_menu(docs) - docs.write_title(str(filename.parent.relative_to(root)).title()) + docs.write_title(str(filename.parent.relative_to(root)) + .replace(os.path.sep, '/').title()) if bots_index: docs.write_text('These are the methods that you may be able to ' @@ -379,19 +380,18 @@ def _write_html_pages(root, tlobjects, methods, layer, input_res): 'telethon.errors.') docs.write_title('Example', id='examples') - docs.write( - '
from telethon.sync import TelegramClient\n'
-                    'from telethon import functions, types\n'
-                    '\n'
-                    'with TelegramClient(name, api_id, api_hash) as client:\n'
-                    '    result = client(')
+                docs.write('''
\
+from telethon.sync import TelegramClient
+from telethon import functions, types
+
+with TelegramClient(name, api_id, api_hash) as client:
+    result = client(''')
                 tlobject.as_example(docs, indent=1)
                 docs.write(')\n')
                 if tlobject.result.startswith('Vector'):
-                    docs.write(
-                        '    for x in result:\n'
-                        '        print(x'
-                    )
+                    docs.write('''\
+    for x in result:
+        print(x''')
                 else:
                     docs.write('    print(result')
                     if tlobject.result != 'Bool' \
diff --git a/telethon_generator/parsers/tlobject/tlarg.py b/telethon_generator/parsers/tlobject/tlarg.py
index 400acc7d..e436d9a2 100644
--- a/telethon_generator/parsers/tlobject/tlarg.py
+++ b/telethon_generator/parsers/tlobject/tlarg.py
@@ -1,6 +1,19 @@
 import re
 
 
+def _fmt_strings(*dicts):
+    for d in dicts:
+        for k, v in d.items():
+            if v in ('None', 'True', 'False'):
+                d[k] = '{}'.format(v)
+            else:
+                d[k] = re.sub(
+                    r'([brf]?([\'"]).*\2)',
+                    lambda m: '{}'.format(m.group(1)),
+                    v
+                )
+
+
 KNOWN_NAMED_EXAMPLES = {
     ('message', 'string'): "'Hello there!'",
     ('expires_at', 'date'): 'datetime.timedelta(minutes=5)',
@@ -41,6 +54,8 @@ KNOWN_TYPED_EXAMPLES = {
     'InputPeer': "'username'"
 }
 
+_fmt_strings(KNOWN_NAMED_EXAMPLES, KNOWN_TYPED_EXAMPLES)
+
 SYNONYMS = {
     'InputUser': 'InputPeer',
     'InputChannel': 'InputPeer',