Includes: INSIDE Limbo Get Playdead's two critically acclaimed titles LIMBO and INSIDE. Limbo - Uncertain of his sister's fate, a boy enters LIMBO INSIDE- Hunted and alone, a boy finds himself drawn into the center of a dark project. INSIDE is a dark, narrative-driven platformer combining intense action with challenging puzzles. It has been critically acclaimed for its moody art style, ambient soundtrack and unsettling atmosphere.
To solve this problem, we need to reformat given JSON input into structured HTML. The JSON input can represent system requirements in different formats, and our task is to convert it into a consistent HTML structure that clearly presents the information.
### Approach
The solution involves the following steps:
1. **Parse the JSON Input**: The JSON input can be structured in two ways:
- As an array of objects where each object contains key-value pairs representing system requirements.
- As an array where each object contains a system type (e.g., Windows, Mac, Linux) and its corresponding requirements provided in HTML format.
2. **Determine the Structure**: Based on the JSON structure, decide whether to generate a single system requirement block or multiple blocks for different systems.
3. **Generate HTML Output**: For each system:
- Extract the system type (if applicable).
- Process the HTML content to extract key-value pairs.
- Convert these pairs into the required HTML structure, ensuring consistent formatting.
4. **Handle Edge Cases**: Ensure that missing data or additional notes are handled gracefully and displayed correctly.
### Solution Code
```python
import json
from bs4 import BeautifulSoup
def reformat_html(json_input):
data = json.loads(json_input)
html_output = []
for item in data:
if 'system' in item:
system_name = item['system']
requirement_html = item['requirement']
soup = BeautifulSoup(requirement_html, 'html.parser')
lis = soup.find_all('li')
requirements = []
for li in lis:
strong = li.find('strong')
if strong:
key = strong.text.strip().rstrip(':')
value = li.text.strip()[len(key)+1:].strip()
requirements.append((key, value))
html_output.append(f'
')
html_output.append(f'
{system_name} System Requirements
')
html_output.append('
')
html_output.append('
MINIMUM SPECS
')
html_output.append('
')
for key, value in requirements:
html_output.append(f' - {key}: {value}
')
html_output.append('
')
html_output.append('
')
html_output.append('
')
else:
product_spec = '
'
product_spec += '
PC System Requirements
'
product_spec += '
'
product_spec += '
MINIMUM SPECS
'
product_spec += '
'
for requirement in item:
key = next(iter(requirement.keys()))
value = requirement[key]
product_spec += f'- {key}: {value}
'
product_spec += '
'
product_spec += '
'
html_output.append(product_spec)
return '\n'.join(html_output).replace('
', '
', 1)
# Example usage:
json_input = '[{"OS":"Windows XP or Windows Vista"},{"Processor":"1.8 GHz"},{"Memory":"512MB RAM (1 GB recommended)"},{"Graphics":"3D graphics card compatible with DirectX 8 (compatible with DirectX 9 recommended)"},{"Hard Drive":"2GB"},{"Additional":"Mouse, Keyboard"}]'
print(reformat_html(json_input))
```
### Explanation
The provided solution uses Python to parse the JSON input and convert it into the required HTML structure. Here's a detailed breakdown:
1. **Parsing JSON**: The JSON input is parsed into a Python data structure for easy manipulation.
2. **Checking Structure**: The code checks if each item in the JSON array contains a 'system' key to determine if it represents a system type (e.g., Windows) or a general product specification.
3. **Processing Requirements**: For each system, the requirement HTML is parsed using BeautifulSoup to extract key-value pairs. These pairs are then formatted into the required HTML structure.
4. **Generating Output**: The formatted HTML is built into a string and returned as the final output.
This approach ensures that the HTML output is clean, well-structured, and consistent, regardless of the input format.
Step-by-Step Explanation of the HTML Reformatting Process:
1. **Conversion of the Outer Ordered List to Unordered List:**
- The root `
` tag is replaced with `
分享