+
+
+class Jinja2Tools:
+ def create_wrmap(self, geojson: Dict) -> str:
+ return create_wrmap(geojson)
+
+ def json_position(self, value: dict) -> str:
+ lon_lat = LonLat(value['longitude'], value['latitude'])
+ return lonlat_to_str(lon_lat)
+
+ def json_pos_ele_position(self, value: dict) -> str:
+ pos = value.get('position')
+ if pos is None:
+ return ''
+ return self.json_position(pos)
+
+ def json_pos_ele_elevation(self, value: dict) -> str:
+ return value.get('elevation', '')
+
+ def list_template(self, name: str, value: List[str]) -> str:
+ return str(wrpylib.mwmarkup.create_template(name, value))
+
+ def json_template(self, value) -> str:
+ args = []
+ kwargs = {}
+ for p in value.get('parameter', []):
+ v = p.get('value', '')
+ if 'key' in p:
+ kwargs[p['key']] = v
+ else:
+ args.append(v)
+ return str(wrpylib.mwmarkup.create_template(value['name'], args, kwargs))
+
+
+def create_sledrun_wiki(sledrun_json: Dict, map_json: Optional[Dict]) -> str:
+ env = jinja2.Environment(
+ loader=jinja2.PackageLoader("wrpylib"),
+ autoescape=jinja2.select_autoescape()
+ )
+ template = env.get_template("sledrun_wiki.txt")
+
+ def markdown_to_mediawiki(markdown: str) -> str:
+ return subprocess.check_output(['pandoc', '--to', 'mediawiki'], input=markdown, encoding='utf-8')
+
+ def position_to_lon_lat(value: Optional[dict]) -> Optional[LonLat]:
+ if value is not None:
+ lon = value.get('longitude')
+ lat = value.get('latitude')
+ if lon is not None and lat is not None:
+ return LonLat(lon, lat)
+ return None
+
+ def position_ele_to_lon_lat(value: Optional[dict]) -> Optional[LonLat]:
+ if value is not None:
+ return position_to_lon_lat(value.get("position"))
+ return None
+
+ def position_ele_to_ele(value: Optional[dict]) -> Optional[int]:
+ if value is not None:
+ ele = value.get('elevation')
+ if ele is not None:
+ return int(ele)
+ return None
+
+ def aufstiegshilfe() -> Optional[List[Tuple[str, Optional[str]]]]:
+ ws = sledrun_json.get('walkup_supports')
+ if ws is None:
+ return None
+ return [(w['type'], w.get('comment')) for w in ws]
+
+ def rodelverleih() -> Optional[List[Tuple[str, Optional[str]]]]:
+ sr = sledrun_json.get('sled_rental_direct')
+ if sr is None:
+ return None
+ return [('Ja', None)] if sr else []
+
+ def webauskunft() -> Tuple[Optional[bool], Optional[str]]:
+ info_web = sledrun_json.get('info_web')
+ if info_web is None:
+ return None, None
+ if len(info_web) == 0:
+ return False, None
+ return True, info_web[0]['url']
+
+ def telefonauskunft() -> Optional[List[Tuple[str, str]]]:
+ info_phone = sledrun_json.get('info_phone')
+ if info_phone is None:
+ return None
+ return [(pc['phone'], pc['name']) for pc in info_phone]
+
+ def betreiber() -> str:
+ has_operator = sledrun_json.get('has_operator')
+ if has_operator is None:
+ return sledrun_json.get('operator')
+ if has_operator:
+ return sledrun_json.get('operator')
+ return 'Nein'
+
+ sledrun_rbb_json = collections.OrderedDict([
+ ('Position', position_to_lon_lat(sledrun_json.get('position'))),
+ ('Position oben', position_ele_to_lon_lat(sledrun_json.get('top'))),
+ ('Höhe oben', position_ele_to_ele(sledrun_json.get('top'))),
+ ('Position unten', position_ele_to_lon_lat(sledrun_json.get('bottom'))),
+ ('Höhe unten', position_ele_to_ele(sledrun_json.get('bottom'))),
+ ('Länge', sledrun_json.get('length')),
+ ('Schwierigkeit', opt_difficulty_german_from_str(sledrun_json.get('difficulty', ''))),
+ ('Lawinen', opt_avalanches_german_from_str(sledrun_json.get('avalanches', ''))),
+ ('Betreiber', betreiber()),
+ ('Öffentliche Anreise', opt_public_transport_german_from_str(sledrun_json.get('public_transport', ''))),
+ ('Aufstieg möglich', sledrun_json.get('walkup_possible')),
+ ('Aufstieg getrennt', opt_tristate_german_comment_from_str(sledrun_json.get('walkup_separate', ''))),
+ ('Gehzeit', sledrun_json.get('walkup_time')),
+ ('Aufstiegshilfe', aufstiegshilfe()),
+ ('Beleuchtungsanlage', opt_tristate_german_comment_from_str(sledrun_json.get('nightlight_possible', ''))),
+ ('Beleuchtungstage', nightlightdays_from_str(sledrun_json.get('nightlight_weekdays', ''))),
+ ('Rodelverleih', rodelverleih()),
+ ('Gütesiegel', None),
+ ('Webauskunft', webauskunft()),
+ ('Telefonauskunft', telefonauskunft()),
+ ('Bild', sledrun_json.get('image')),
+ ('In Übersichtskarte', sledrun_json.get('show_in_overview')),
+ ('Forumid', sledrun_json.get('forum_id'))
+ ])
+
+ def get_markdown_field(key: str) -> str:
+ if key in sledrun_json:
+ return markdown_to_mediawiki(sledrun_json[key])
+ return ''
+
+ description = get_markdown_field('description').strip()
+ night_light = get_markdown_field('night_light').strip()
+ sled_rental_description = get_markdown_field('sled_rental_description').strip()
+
+ rodelbahnbox = rodelbahnbox_to_str(sledrun_rbb_json)
+
+ return template.render(sledrun_json=sledrun_json,
+ rodelbahnbox=rodelbahnbox, description=description, night_light=night_light,
+ sled_rental_description=sled_rental_description, operator=betreiber(),
+ map_json=map_json, h=Jinja2Tools())