Coverage for fastblocks / adapters / style / webawesome.py: 58%

140 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-02 20:00 -0800

1"""WebAwesome styles adapter for FastBlocks with integrated icon system.""" 

2 

3from contextlib import suppress 

4from typing import Any 

5from uuid import UUID 

6 

7from oneiric.core.resolution import Resolver 

8 

9from ._base import StyleBase, StyleBaseSettings 

10 

11# Oneiric resolver for dependency injection 

12depends = Resolver() 

13 

14 

15class WebAwesomeStyleSettings(StyleBaseSettings): 

16 """Settings for WebAwesome styles adapter.""" 

17 

18 # Required ACB 0.19.0+ metadata 

19 MODULE_ID: UUID = UUID("01937d86-7a5c-9f6d-b8e9-d7e8f9a0b1c2") # Static UUID7 

20 MODULE_STATUS: str = "stable" 

21 

22 # WebAwesome configuration 

23 version: str = "latest" 

24 cdn_url: str = "https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free" 

25 include_brands: bool = True 

26 include_regular: bool = True 

27 include_solid: bool = True 

28 

29 # Custom configuration 

30 custom_css_url: str | None = None 

31 primary_color: str = "#007bff" 

32 secondary_color: str = "#6c757d" 

33 success_color: str = "#28a745" 

34 warning_color: str = "#ffc107" 

35 danger_color: str = "#dc3545" 

36 info_color: str = "#17a2b8" 

37 

38 # Layout settings 

39 container_max_width: str = "1200px" 

40 grid_columns: int = 12 

41 gutter_width: str = "1rem" 

42 

43 # Typography 

44 font_family: str = ( 

45 "-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif" 

46 ) 

47 base_font_size: str = "16px" 

48 line_height: str = "1.6" 

49 

50 

51class WebAwesomeStyle(StyleBase): 

52 """WebAwesome styles adapter with integrated icons and components.""" 

53 

54 # Required ACB 0.19.0+ metadata 

55 MODULE_ID: UUID = UUID("01937d86-7a5c-9f6d-b8e9-d7e8f9a0b1c2") # Static UUID7 

56 MODULE_STATUS: str = "stable" 

57 

58 def __init__(self) -> None: 

59 """Initialize WebAwesome adapter.""" 

60 super().__init__() 

61 self.settings: WebAwesomeStyleSettings | None = None 

62 

63 # Register with Oneiric resolver (fail gracefully if not supported) 

64 with suppress(Exception): 

65 depends.set(self) 

66 

67 def get_stylesheet_links(self) -> list[str]: 

68 """Get WebAwesome stylesheet links.""" 

69 if not self.settings: 

70 self.settings = WebAwesomeStyleSettings() 

71 

72 links = [] 

73 

74 # FontAwesome CSS (for icon integration) 

75 if self.settings.include_solid: 

76 links.extend( 

77 ( 

78 f'<link rel="stylesheet" href="{self.settings.cdn_url}@{self.settings.version}/css/fontawesome.min.css">', 

79 f'<link rel="stylesheet" href="{self.settings.cdn_url}@{self.settings.version}/css/solid.min.css">', 

80 ) 

81 ) 

82 

83 if self.settings.include_regular: 

84 links.append( 

85 f'<link rel="stylesheet" href="{self.settings.cdn_url}@{self.settings.version}/css/regular.min.css">' 

86 ) 

87 

88 if self.settings.include_brands: 

89 links.append( 

90 f'<link rel="stylesheet" href="{self.settings.cdn_url}@{self.settings.version}/css/brands.min.css">' 

91 ) 

92 

93 # Custom WebAwesome CSS 

94 if self.settings.custom_css_url: 

95 links.append( 

96 f'<link rel="stylesheet" href="{self.settings.custom_css_url}">' 

97 ) 

98 

99 # Generate inline CSS for WebAwesome system 

100 inline_css = self._generate_webawesome_css() 

101 links.append(f"<style>{inline_css}</style>") 

102 

103 return links 

104 

105 def _generate_webawesome_css(self) -> str: 

106 """Generate WebAwesome CSS framework.""" 

107 if not self.settings: 

108 self.settings = WebAwesomeStyleSettings() 

109 

110 css = f""" 

111/* WebAwesome CSS Framework for FastBlocks */ 

112:root {{ 

113 --wa-primary: {self.settings.primary_color}; 

114 --wa-secondary: {self.settings.secondary_color}; 

115 --wa-success: {self.settings.success_color}; 

116 --wa-warning: {self.settings.warning_color}; 

117 --wa-danger: {self.settings.danger_color}; 

118 --wa-info: {self.settings.info_color}; 

119 --wa-font-family: {self.settings.font_family}; 

120 --wa-font-size: {self.settings.base_font_size}; 

121 --wa-line-height: {self.settings.line_height}; 

122 --wa-container-max-width: {self.settings.container_max_width}; 

123 --wa-gutter: {self.settings.gutter_width}; 

124}} 

125 

126/* Reset and Base */ 

127*, *::before, *::after {{ 

128 box-sizing: border-box; 

129}} 

130 

131body {{ 

132 font-family: var(--wa-font-family); 

133 font-size: var(--wa-font-size); 

134 line-height: var(--wa-line-height); 

135 margin: 0; 

136 padding: 0; 

137}} 

138 

139/* Container System */ 

140.wa-container {{ 

141 max-width: var(--wa-container-max-width); 

142 margin: 0 auto; 

143 padding: 0 var(--wa-gutter); 

144}} 

145 

146.wa-container-fluid {{ 

147 width: 100%; 

148 padding: 0 var(--wa-gutter); 

149}} 

150 

151/* Grid System */ 

152.wa-row {{ 

153 display: flex; 

154 flex-wrap: wrap; 

155 margin: 0 calc(var(--wa-gutter) / -2); 

156}} 

157 

158.wa-col {{ 

159 flex: 1; 

160 padding: 0 calc(var(--wa-gutter) / 2); 

161}} 

162 

163/* Responsive columns */ 

164{self._generate_grid_css()} 

165 

166/* Component System */ 

167.wa-card {{ 

168 background: white; 

169 border: 1px solid #e9ecef; 

170 border-radius: 0.5rem; 

171 box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075); 

172 overflow: hidden; 

173}} 

174 

175.wa-card-header {{ 

176 padding: 1rem; 

177 background: #f8f9fa; 

178 border-bottom: 1px solid #e9ecef; 

179 font-weight: 600; 

180}} 

181 

182.wa-card-body {{ 

183 padding: 1rem; 

184}} 

185 

186.wa-card-footer {{ 

187 padding: 1rem; 

188 background: #f8f9fa; 

189 border-top: 1px solid #e9ecef; 

190}} 

191 

192/* Button System */ 

193.wa-btn {{ 

194 display: inline-block; 

195 padding: 0.5rem 1rem; 

196 border: 1px solid transparent; 

197 border-radius: 0.375rem; 

198 font-weight: 500; 

199 text-align: center; 

200 text-decoration: none; 

201 cursor: pointer; 

202 transition: all 0.2s ease-in-out; 

203 font-family: inherit; 

204 font-size: 1rem; 

205 line-height: 1.5; 

206}} 

207 

208.wa-btn:hover {{ 

209 transform: translateY(-1px); 

210 box-shadow: 0 0.125rem 0.5rem rgba(0, 0, 0, 0.15); 

211}} 

212 

213.wa-btn-primary {{ 

214 background: var(--wa-primary); 

215 border-color: var(--wa-primary); 

216 color: white; 

217}} 

218 

219.wa-btn-secondary {{ 

220 background: var(--wa-secondary); 

221 border-color: var(--wa-secondary); 

222 color: white; 

223}} 

224 

225.wa-btn-success {{ 

226 background: var(--wa-success); 

227 border-color: var(--wa-success); 

228 color: white; 

229}} 

230 

231.wa-btn-warning {{ 

232 background: var(--wa-warning); 

233 border-color: var(--wa-warning); 

234 color: #212529; 

235}} 

236 

237.wa-btn-danger {{ 

238 background: var(--wa-danger); 

239 border-color: var(--wa-danger); 

240 color: white; 

241}} 

242 

243.wa-btn-info {{ 

244 background: var(--wa-info); 

245 border-color: var(--wa-info); 

246 color: white; 

247}} 

248 

249/* Form Controls */ 

250.wa-form-group {{ 

251 margin-bottom: 1rem; 

252}} 

253 

254.wa-form-label {{ 

255 display: block; 

256 margin-bottom: 0.5rem; 

257 font-weight: 500; 

258 color: #495057; 

259}} 

260 

261.wa-form-control {{ 

262 display: block; 

263 width: 100%; 

264 padding: 0.5rem 0.75rem; 

265 font-size: 1rem; 

266 line-height: 1.5; 

267 color: #495057; 

268 background: white; 

269 border: 1px solid #ced4da; 

270 border-radius: 0.375rem; 

271 transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out; 

272}} 

273 

274.wa-form-control:focus {{ 

275 border-color: var(--wa-primary); 

276 outline: 0; 

277 box-shadow: 0 0 0 0.2rem rgba(0, 123, 255, 0.25); 

278}} 

279 

280/* Alert System */ 

281.wa-alert {{ 

282 padding: 1rem; 

283 margin-bottom: 1rem; 

284 border: 1px solid transparent; 

285 border-radius: 0.375rem; 

286}} 

287 

288.wa-alert-primary {{ 

289 color: #084298; 

290 background: #cfe2ff; 

291 border-color: #b6d4fe; 

292}} 

293 

294.wa-alert-success {{ 

295 color: #0f5132; 

296 background: #d1e7dd; 

297 border-color: #badbcc; 

298}} 

299 

300.wa-alert-warning {{ 

301 color: #664d03; 

302 background: #fff3cd; 

303 border-color: #ffecb5; 

304}} 

305 

306.wa-alert-danger {{ 

307 color: #842029; 

308 background: #f8d7da; 

309 border-color: #f5c2c7; 

310}} 

311 

312/* Navigation */ 

313.wa-navbar {{ 

314 display: flex; 

315 align-items: center; 

316 justify-content: space-between; 

317 padding: 1rem var(--wa-gutter); 

318 background: white; 

319 border-bottom: 1px solid #e9ecef; 

320}} 

321 

322.wa-navbar-brand {{ 

323 font-size: 1.25rem; 

324 font-weight: 600; 

325 text-decoration: none; 

326 color: var(--wa-primary); 

327}} 

328 

329.wa-navbar-nav {{ 

330 display: flex; 

331 list-style: none; 

332 margin: 0; 

333 padding: 0; 

334 gap: 1rem; 

335}} 

336 

337.wa-navbar-link {{ 

338 text-decoration: none; 

339 color: #495057; 

340 transition: color 0.2s; 

341}} 

342 

343.wa-navbar-link:hover {{ 

344 color: var(--wa-primary); 

345}} 

346 

347/* Icon Integration */ 

348.wa-icon {{ 

349 display: inline-block; 

350 width: 1em; 

351 height: 1em; 

352 vertical-align: -0.125em; 

353}} 

354 

355.wa-icon-sm {{ 

356 font-size: 0.875rem; 

357}} 

358 

359.wa-icon-lg {{ 

360 font-size: 1.125rem; 

361}} 

362 

363.wa-icon-xl {{ 

364 font-size: 1.5rem; 

365}} 

366 

367.wa-icon-2x {{ 

368 font-size: 2rem; 

369}} 

370 

371/* Utility Classes */ 

372.wa-text-center {{ text-align: center; }} 

373.wa-text-left {{ text-align: left; }} 

374.wa-text-right {{ text-align: right; }} 

375 

376.wa-d-block {{ display: block; }} 

377.wa-d-inline {{ display: inline; }} 

378.wa-d-inline-block {{ display: inline-block; }} 

379.wa-d-flex {{ display: flex; }} 

380.wa-d-none {{ display: none; }} 

381 

382.wa-mt-0 {{ margin-top: 0; }} 

383.wa-mt-1 {{ margin-top: 0.25rem; }} 

384.wa-mt-2 {{ margin-top: 0.5rem; }} 

385.wa-mt-3 {{ margin-top: 1rem; }} 

386.wa-mt-4 {{ margin-top: 1.5rem; }} 

387.wa-mt-5 {{ margin-top: 3rem; }} 

388 

389.wa-mb-0 {{ margin-bottom: 0; }} 

390.wa-mb-1 {{ margin-bottom: 0.25rem; }} 

391.wa-mb-2 {{ margin-bottom: 0.5rem; }} 

392.wa-mb-3 {{ margin-bottom: 1rem; }} 

393.wa-mb-4 {{ margin-bottom: 1.5rem; }} 

394.wa-mb-5 {{ margin-bottom: 3rem; }} 

395 

396.wa-p-0 {{ padding: 0; }} 

397.wa-p-1 {{ padding: 0.25rem; }} 

398.wa-p-2 {{ padding: 0.5rem; }} 

399.wa-p-3 {{ padding: 1rem; }} 

400.wa-p-4 {{ padding: 1.5rem; }} 

401.wa-p-5 {{ padding: 3rem; }} 

402 

403/* Responsive Design */ 

404@media (max-width: 768px) {{ 

405 .wa-container {{ 

406 padding: 0 0.5rem; 

407 }} 

408 

409 .wa-btn {{ 

410 width: 100%; 

411 margin-bottom: 0.5rem; 

412 }} 

413 

414 .wa-navbar {{ 

415 flex-direction: column; 

416 gap: 1rem; 

417 }} 

418}} 

419""" 

420 return css 

421 

422 def _generate_grid_css(self) -> str: 

423 """Generate responsive grid CSS.""" 

424 if not self.settings: 

425 self.settings = WebAwesomeStyleSettings() 

426 

427 css = "" 

428 breakpoints = { 

429 "sm": "576px", 

430 "md": "768px", 

431 "lg": "992px", 

432 "xl": "1200px", 

433 } 

434 

435 for _breakpoint, width in breakpoints.items(): 

436 css += f"\n@media (min-width: {width}) {{\n" 

437 

438 for i in range(1, self.settings.grid_columns + 1): 

439 percentage = (i / self.settings.grid_columns) * 100 

440 css += ( 

441 f" .wa-col-{_breakpoint}-{i} {{ flex: 0 0 {percentage:.4f}%; " 

442 f"max-width: {percentage:.4f}%; }}\n" 

443 ) 

444 

445 css += "}\n" 

446 

447 # Default columns 

448 for i in range(1, self.settings.grid_columns + 1): 

449 percentage = (i / self.settings.grid_columns) * 100 

450 css += f".wa-col-{i} {{ flex: 0 0 {percentage:.4f}%; max-width: {percentage:.4f}%; }}\n" 

451 

452 return css 

453 

454 def get_component_class(self, component: str) -> str: 

455 """Get WebAwesome-specific classes.""" 

456 class_map = { 

457 # Layout 

458 "container": "wa-container", 

459 "container-fluid": "wa-container-fluid", 

460 "row": "wa-row", 

461 "col": "wa-col", 

462 # Components 

463 "card": "wa-card", 

464 "card-header": "wa-card-header", 

465 "card-body": "wa-card-body", 

466 "card-footer": "wa-card-footer", 

467 # Buttons 

468 "button": "wa-btn wa-btn-primary", 

469 "btn": "wa-btn", 

470 "btn-primary": "wa-btn wa-btn-primary", 

471 "btn-secondary": "wa-btn wa-btn-secondary", 

472 "btn-success": "wa-btn wa-btn-success", 

473 "btn-warning": "wa-btn wa-btn-warning", 

474 "btn-danger": "wa-btn wa-btn-danger", 

475 "btn-info": "wa-btn wa-btn-info", 

476 # Forms 

477 "form-group": "wa-form-group", 

478 "form-label": "wa-form-label", 

479 "form-control": "wa-form-control", 

480 "input": "wa-form-control", 

481 "textarea": "wa-form-control", 

482 "select": "wa-form-control", 

483 # Alerts 

484 "alert": "wa-alert", 

485 "alert-primary": "wa-alert wa-alert-primary", 

486 "alert-success": "wa-alert wa-alert-success", 

487 "alert-warning": "wa-alert wa-alert-warning", 

488 "alert-danger": "wa-alert wa-alert-danger", 

489 # Navigation 

490 "navbar": "wa-navbar", 

491 "navbar-brand": "wa-navbar-brand", 

492 "navbar-nav": "wa-navbar-nav", 

493 "navbar-link": "wa-navbar-link", 

494 # Icons 

495 "icon": "wa-icon fas", 

496 "icon-sm": "wa-icon wa-icon-sm fas", 

497 "icon-lg": "wa-icon wa-icon-lg fas", 

498 "icon-xl": "wa-icon wa-icon-xl fas", 

499 "icon-2x": "wa-icon wa-icon-2x fas", 

500 } 

501 

502 return class_map.get(component, f"wa-{component}") 

503 

504 @staticmethod 

505 def get_icon_class(icon_name: str, style: str = "solid") -> str: 

506 """Get FontAwesome icon class integrated with WebAwesome.""" 

507 prefix_map = { 

508 "solid": "fas", 

509 "regular": "far", 

510 "brands": "fab", 

511 } 

512 

513 prefix = prefix_map.get(style, "fas") 

514 

515 # Ensure icon name has fa- prefix 

516 if not icon_name.startswith("fa-"): 

517 icon_name = f"fa-{icon_name}" 

518 

519 return f"wa-icon {prefix} {icon_name}" 

520 

521 

522# Template function registration for FastBlocks 

523def _register_wa_basic_filters(env: Any) -> None: 

524 """Register basic WebAwesome filters.""" 

525 

526 @env.global_("wa_stylesheet_links") # type: ignore[misc] 

527 def wa_stylesheet_links() -> str: 

528 """Global function for WebAwesome stylesheet links.""" 

529 styles = depends.get_sync("styles") 

530 if isinstance(styles, WebAwesomeStyle): 

531 return "\n".join(styles.get_stylesheet_links()) 

532 return "" 

533 

534 @env.filter("wa_class") # type: ignore[misc] 

535 def wa_class_filter(component: str) -> str: 

536 """Filter for getting WebAwesome component classes.""" 

537 styles = depends.get_sync("styles") 

538 if isinstance(styles, WebAwesomeStyle): 

539 return styles.get_component_class(component) 

540 return component 

541 

542 @env.filter("wa_icon") # type: ignore[misc] 

543 def wa_icon_filter(icon_name: str, style: str = "solid") -> str: 

544 """Filter for WebAwesome icon classes.""" 

545 styles = depends.get_sync("styles") 

546 if isinstance(styles, WebAwesomeStyle): 

547 return styles.get_icon_class(icon_name, style) 

548 return f"fa-{icon_name}" 

549 

550 

551def _register_wa_button_functions(env: Any) -> None: 

552 """Register WebAwesome button component functions.""" 

553 

554 @env.global_("wa_button") # type: ignore[misc] 

555 def wa_button( 

556 text: str, variant: str = "primary", icon: str | None = None, **attributes: Any 

557 ) -> str: 

558 """Generate WebAwesome button with optional icon.""" 

559 styles = depends.get_sync("styles") 

560 if not isinstance(styles, WebAwesomeStyle): 

561 return f'<button class="btn">{text}</button>' 

562 

563 btn_class = styles.get_component_class(f"btn-{variant}") 

564 if "class" in attributes: 

565 btn_class += f" {attributes.pop('class')}" 

566 

567 content = "" 

568 if icon: 

569 content += f'<i class="{styles.get_icon_class(icon)}"></i> ' 

570 content += text 

571 

572 attr_string = " ".join(f'{k}="{v}"' for k, v in attributes.items()) 

573 return f'<button class="{btn_class}" {attr_string}>{content}</button>' 

574 

575 

576def _register_wa_card_functions(env: Any) -> None: 

577 """Register WebAwesome card component functions.""" 

578 

579 @env.global_("wa_card") # type: ignore[misc] 

580 def wa_card( 

581 title: str | None = None, 

582 content: str = "", 

583 footer: str | None = None, 

584 **attributes: Any, 

585 ) -> str: 

586 """Generate WebAwesome card component.""" 

587 styles = depends.get_sync("styles") 

588 if not isinstance(styles, WebAwesomeStyle): 

589 return f'<div class="card">{content}</div>' 

590 

591 card_class = styles.get_component_class("card") 

592 if "class" in attributes: 

593 card_class += f" {attributes.pop('class')}" 

594 

595 card_content = "" 

596 if title: 

597 card_content += f'<div class="{styles.get_component_class("card-header")}">{title}</div>' 

598 card_content += ( 

599 f'<div class="{styles.get_component_class("card-body")}">{content}</div>' 

600 ) 

601 if footer: 

602 card_content += f'<div class="{styles.get_component_class("card-footer")}">{footer}</div>' 

603 

604 attr_string = " ".join(f'{k}="{v}"' for k, v in attributes.items()) 

605 return f'<div class="{card_class}" {attr_string}>{card_content}</div>' 

606 

607 

608def register_webawesome_functions(env: Any) -> None: 

609 """Register WebAwesome functions for Jinja2 templates.""" 

610 _register_wa_basic_filters(env) 

611 _register_wa_button_functions(env) 

612 _register_wa_card_functions(env) 

613 

614 

615StyleSettings = WebAwesomeStyleSettings 

616Style = WebAwesomeStyle 

617 

618# Register with Oneiric resolver (fail gracefully if not supported) 

619with suppress(Exception): 

620 depends.set(Style, "webawesome") 

621 

622# ACB 0.19.0+ compatibility 

623__all__ = [ 

624 "WebAwesomeStyle", 

625 "WebAwesomeStyleSettings", 

626 "register_webawesome_functions", 

627 "Style", 

628 "StyleSettings", 

629]