def assert_html2rest(self, rest_string, html_string, strip_lines=False, debug=False, parser_kwargs={}, emitter_kwargs={}): """ Check html to reStructuredText converter """ self.assertNotEqual(rest_string, html_string) rest_string = self._prepare_text(rest_string) print("-" * 100) print(rest_string) html_string = self._prepare_text(html_string) if strip_lines: html_string = strip_html_lines(html_string, strip_lines) print("-" * 100) print(html_string) # compare html -> reStructuredText rest_string2 = html2rest(html_string, debug, parser_kwargs, emitter_kwargs) print("-" * 100) print(rest_string2) print("-" * 100) self.assertEqual(rest_string2, rest_string, msg="html2rest") return rest_string, html_string
def get_long_description(package_root, filename="README.creole", raise_errors=None): """ read README file and convert it on-the-fly to ReStructuredText. """ if raise_errors is None: raise_errors = should_raise_errors() if raise_errors: sys.stderr.write("Test creole2rest and raise an error, if rendering failed...\n") # raise a error if a unknown node found unknown_emit = raise_unknown_node else: # ignore unknown nodes unknown_emit = transparent_unknown_nodes filepath = os.path.join(package_root, filename) long_description_origin = "" try: # Read creole README f = codecs.open(filepath, "r", encoding="utf-8") long_description_origin = f.read().strip() f.close() # convert creole into html long_description_html = creole2html(long_description_origin) # convert html to ReSt long_description_rest_unicode = html2rest( long_description_html, emitter_kwargs={"unknown_emit":unknown_emit} ) if PY3: long_description_rest = long_description_rest_unicode else: long_description_rest = long_description_rest_unicode.encode("utf-8") except Exception: if raise_errors: raise # Don't raise the error e.g. in ./setup install process evalue = sys.exc_info()[1] long_description_rest = "[Error: %s]\n%s" % ( evalue, long_description_origin ) else: if raise_errors: # Test created ReSt code like PyPi does it. from creole.rest_tools.pypi_rest2html import pypi_rest2html try: pypi_rest2html(long_description_rest_unicode) except SystemExit as e: msg = "Error creole2rest self test failed: rest2html() exist with status code: %s\n" % e.args[0] sys.stderr.write(msg) sys.exit(msg) except Exception as e: sys.exit("ReSt2html error: %s" % e) else: if "check" in sys.argv: print("Generating creole to ReSt to html, ok.") return long_description_rest
def get_long_description(package_root, filename='README.creole', raise_errors=None): """ read README file and convert it on-the-fly to ReStructuredText. """ warnings.warn('get_long_description() will be removed in the future', DeprecationWarning) if raise_errors is None: raise_errors = should_raise_errors() if raise_errors: sys.stderr.write( 'Test creole2rest and raise an error, if rendering failed...\n') # raise a error if a unknown node found unknown_emit = raise_unknown_node else: # ignore unknown nodes unknown_emit = transparent_unknown_nodes filepath = os.path.join(package_root, filename) long_description_origin = '' try: # Read creole README f = codecs.open(filepath, 'r', encoding='utf-8') long_description_origin = f.read().strip() f.close() # convert creole into html long_description_html = creole2html(long_description_origin) # convert html to ReSt long_description_rest = html2rest( long_description_html, emitter_kwargs={'unknown_emit': unknown_emit}) except Exception: if raise_errors: raise # Don't raise the error e.g. in ./setup install process evalue = sys.exc_info()[1] long_description_rest = f'[Error: {evalue}]\n{long_description_origin}' else: if raise_errors: # Test created ReSt code like PyPi does it. from creole.rest_tools.pypi_rest2html import pypi_rest2html try: pypi_rest2html(long_description_rest) except SystemExit as e: msg = f'Error creole2rest self test failed: rest2html() exist with status code: {e.args[0]}\n' sys.stderr.write(msg) sys.exit(msg) except Exception as e: sys.exit(f'ReSt2html error: {e}') else: if 'check' in sys.argv: print('Generating creole to ReSt to html, ok.') return long_description_rest
def get_long_description(package_root, filename="README.creole", raise_errors=None): """ read README file and convert it on-the-fly to ReStructuredText. """ if raise_errors is None: raise_errors = should_raise_errors() if raise_errors: # raise a error if a unknown node found unknown_emit = raise_unknown_node else: # ignore unknown nodes unknown_emit = transparent_unknown_nodes filepath = os.path.join(package_root, filename) long_description_origin = "" try: # Read creole README f = codecs.open(filepath, "r", encoding="utf-8") long_description_origin = f.read().strip() f.close() # convert creole into html long_description_html = creole2html(long_description_origin) # convert html to ReSt long_description_rest_unicode = html2rest( long_description_html, emitter_kwargs={"unknown_emit":unknown_emit} ) if PY3: long_description_rest = long_description_rest_unicode else: long_description_rest = long_description_rest_unicode.encode("utf-8") except Exception: if raise_errors: raise # Don't raise the error e.g. in ./setup install process evalue = sys.exc_info()[1] long_description_rest = "[Error: %s]\n%s" % ( evalue, long_description_origin ) else: if raise_errors: # Test created ReSt code from creole.rest2html.clean_writer import rest2html try: rest2html(long_description_rest_unicode, traceback=True, enable_exit_status=1, exit_status_level=2) except SystemExit as e: print("Error creole2rest self test failed: rest2html() exist with status code: %s" % e.args[0]) raise return long_description_rest
def _generate_rst_readme(*, creole_readme_path): with creole_readme_path.open('r') as f: creole_readme = f.read().strip() # convert creole into html html_readme = creole2html(creole_readme) # convert html to ReSt rest_readme = html2rest( html_readme, emitter_kwargs={ 'unknown_emit': raise_unknown_node # raise a error if a unknown node found }) return rest_readme
def _generate_rst_readme(*, creole_readme_path): with creole_readme_path.open('r') as f: creole_readme = f.read().strip() # convert creole into html html_readme = creole2html(creole_readme) # convert html to ReSt rest_readme = html2rest( html_readme, emitter_kwargs={ 'unknown_emit': raise_unknown_node # raise a error if a unknown node found }) # Check if generated ReSt is valid, see also: # https://pypi.org/help/#description-content-type rendered = render(rest_readme, stream=sys.stderr) if rendered is None: sys.exit(1) return rest_readme
def assert_html2rest(self, rest_string, html_string, \ strip_lines=False, debug=False, parser_kwargs={}, emitter_kwargs={}): """ Check html to reStructuredText converter """ self.assertNotEqual(rest_string, html_string) rest_string = self._prepare_text(rest_string) html_string = self._prepare_text(html_string) if strip_lines: html_string = strip_html_lines(html_string, strip_lines) # compare html -> reStructuredText rest_string2 = html2rest(html_string, debug, parser_kwargs, emitter_kwargs) if debug: print("-" * 79) print(rest_string2) print("-" * 79) self.assertEqual(rest_string2, rest_string, msg="html2rest") return rest_string, html_string
=== a table: |=headline 1 |= headline 2 | | 1.1. cell | 1.2. cell | | 2.1. cell | 2.2. cell | ---- More info on our [[http://code.google.com/p/python-creole/|Homepage]].""" if __name__ == "__main__": print("_" * 79 + "\n*** Convert creole into html: ***\n\n") html = creole2html(source_creole) print(html) print("\n\n" + "_" * 79 + "\n*** Convert html back into creole: ***\n\n") creole = html2creole(html) print(creole) print("\n\n" + "_" * 79 + "\n*** Convert html into ReStructuredText: ***\n\n") rest = html2rest(html) print(rest) print("\n\n" + "_" * 79 + "\n*** Convert html into textile: ***\n\n") textile = html2textile(html) print(textile)
def main(group_id, location, time_boundary, event_status, pandoc, force): key_path = os.path.normpath(os.path.expanduser('~/.meetup.com-key')) if os.path.exists(key_path): with io.open(key_path, encoding='utf8') as fh: key = fh.read().strip() else: key = None cache = FileCache('.web_cache', forever=True) requests = CacheControl(Session(), cache, cache_etags=False, heuristic=ExpiresAfter(days=1)) while True: resp = requests.get('https://api.meetup.com/status', params=dict(key=key)) if resp.status_code == 200 and resp.json().get('status') == 'ok': break elif resp.status_code == 200 and any( 'auth_fail' == e.code for e in resp.json().get('errors', [])): click.echo( 'Your meetup.com key is required. You can get it from https://secure.meetup.com/meetup_api/key/\n' ) if click.confirm( 'Open https://secure.meetup.com/meetup_api/key/ in your web browser?' ): click.launch('https://secure.meetup.com/meetup_api/key/') click.echo('') key = click.prompt('Key', hide_input=True) else: raise click.ClickException( 'Failed to get meetup.com status. Response was {!r} {!r}'. format(resp.status_code, resp.text)) click.secho( 'For convenience your key is saved in `{}`.\n'.format(key_path), fg='magenta') with open(key_path, 'w') as fh: fh.write(key) while not location: location = location or get_input( u'Location: ', completer=WordCompleter( [u'cluj', u'iasi', u'timisoara', u'bucuresti'], ignore_case=True)) while True: group_id = group_id or get_input( u'Group ID: ', completer=WordCompleter([ u'RoPython-Bucuresti', u'RoPython-Cluj', u'RoPython_Iasi', u'RoPython-Timisoara' ], ignore_case=True)) resp = requests.get('https://api.meetup.com/2/events', params=dict( key=key, group_urlname=group_id, time=time_boundary, status=event_status, )) if resp.status_code == 200: json = resp.json() if json['results']: break else: click.secho( 'Invalid group `{}`. It has no events!'.format(group_id), fg='red') group_id = None if resp.status_code == '400': click.fail( 'Failed to get make correct request. Response was {!r}'.format( resp.text)) else: click.secho('Invalid group `{}`. Response was [{}] {!r}'.format( group_id, resp.status_code, resp.text), fg='red') # click.echo(pformat(dict(resp.headers))) for event in json['results']: dt = datetime.fromtimestamp(event['time'] / 1000) event['duration'] = format_duration( event.get('duration', 3600000) / 1000) event['time'] = dt.strftime('%Y-%m-%d %H:%M') if 'how_to_find_us' in event: address = event['how_to_find_us'], else: address = () if 'venue' in event: address_1 = event['venue'].get('address_1') if address_1: address += address_1, event['venue']['address_1'] = ', '.join(address) else: event['venue'] = {'address_1': address} click.echo("{time}: {name}".format(**event)) click.echo("\t{}".format(pformat(event))) existing_path = glob( os.path.join('content', '*', dt.strftime('%Y-%m-%d*'), 'index.rst')) if existing_path and not force: if len(existing_path) > 1: click.secho('\tERROR: multiple paths matched: {}'.format( existing_path)) else: click.secho('\t`{}` already exists. Not importing.'.format( *existing_path), fg='yellow') else: target_dir = os.path.join( 'content', location, '{}-{}'.format(dt.strftime('%Y-%m-%d'), slugify(event['name']))) target_path = os.path.join(target_dir, 'index.rst') if not os.path.exists(target_dir): os.makedirs(target_dir) if pandoc: with tempfile.NamedTemporaryFile(delete=False) as fh: fh.write(event['description'].encode('utf-8')) rst = subprocess.check_output( ['pandoc', '--from=html', '--to=rst', fh.name]).decode('utf-8') os.unlink(fh.name) else: rst = html2rest(event['description']) doc = u'''{name} ############################################################### :tags: prezentari :registration: meetup.com: {event_url} :start: {time} :duration: {duration} :location: {venue[address_1]}, {venue[city]}, {venue[localized_country_name]} {rst}'''.format(rst=rst, **event) with io.open(target_path, 'w', encoding='utf-8') as fh: fh.write(doc) click.secho('\tWrote `{}`.'.format(target_path), fg='green')