Add a nice button to the docs to copy the import code

This commit is contained in:
Lonami Exo 2017-06-04 19:57:20 +02:00
parent cf6f300df1
commit 2f00033683
3 changed files with 61 additions and 0 deletions

View File

@ -24,6 +24,7 @@ class DocsWriter:
self.menu_began = False self.menu_began = False
self.table_columns = 0 self.table_columns = 0
self.table_columns_left = None self.table_columns_left = None
self.write_copy_script = False
# High level writing # High level writing
def write_head(self, title, relative_css_path): def write_head(self, title, relative_css_path):
@ -232,8 +233,27 @@ class DocsWriter:
self.write(text) self.write(text)
self.write('</p>') self.write('</p>')
def write_copy_button(self, text, text_to_copy):
"""Writes a button with 'text' which can be used
to copy 'text_to_copy' to clipboard when it's clicked."""
self.write_copy_script = True
self.write('<button onclick="cp(\'{}\');">{}</button>'
.format(text_to_copy, text))
def end_body(self): def end_body(self):
"""Ends the whole document. This should be called the last""" """Ends the whole document. This should be called the last"""
if self.write_copy_script:
self.write(
'<textarea id="c" class="invisible"></textarea>'
'<script>'
'function cp(t){'
'var c=document.getElementById("c");'
'c.value=t;'
'c.select();'
'try{document.execCommand("copy")}'
'catch(e){}}'
'</script>')
self.write('</div></body></html>') self.write('</div></body></html>')
# "Low" level writing # "Low" level writing

View File

@ -49,6 +49,15 @@ def get_file_name(tlobject, add_extension=False):
return result return result
# TLObject -> from ... import ...
def get_import_code(tlobject):
kind = 'functions' if tlobject.is_function else 'types'
ns = '.' + tlobject.namespace if tlobject.namespace else ''
return 'from telethon.tl.{}{} import {}'\
.format(kind, ns, get_class_name(tlobject))
def get_create_path_for(tlobject): def get_create_path_for(tlobject):
"""Gets the file path (and creates the parent directories) """Gets the file path (and creates the parent directories)
for the given 'tlobject', relative to nothing; only its local path""" for the given 'tlobject', relative to nothing; only its local path"""
@ -229,6 +238,8 @@ def generate_documentation(scheme_file):
# Write the code definition for this TLObject # Write the code definition for this TLObject
docs.write_code(tlobject) docs.write_code(tlobject)
docs.write_copy_button('Copy import to the clipboard',
get_import_code(tlobject))
# Write the return type (or constructors belonging to the same type) # Write the return type (or constructors belonging to the same type)
docs.write_title('Returns' if tlobject.is_function docs.write_title('Returns' if tlobject.is_function

View File

@ -122,6 +122,36 @@ span.sh4 {
font-style: italic; font-style: italic;
} }
button {
border-radius: 2px;
font-size: 16px;
padding: 8px;
color: #000;
background-color: #fff;
border: 2px solid #42aaed;
transition-duration: 300ms;
}
button:hover {
background-color: #42aaed;
color: #fff;
}
/* https://stackoverflow.com/a/30810322 */
.invisible {
left: 0;
top: -99px;
padding: 0;
width: 2em;
height: 2em;
border: none;
outline: none;
position: fixed;
box-shadow: none;
color: transparent;
background: transparent;
}
@media (max-width: 640px) { @media (max-width: 640px) {
h1 { h1 {
font-size: 18px; font-size: 18px;