Generate_CSS_PropertyID_h.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ByteBuffer.h>
  7. #include <AK/JsonObject.h>
  8. #include <AK/SourceGenerator.h>
  9. #include <AK/StringBuilder.h>
  10. #include <LibCore/File.h>
  11. #include <ctype.h>
  12. #include <stdio.h>
  13. static String title_casify(const String& dashy_name)
  14. {
  15. auto parts = dashy_name.split('-');
  16. StringBuilder builder;
  17. for (auto& part : parts) {
  18. if (part.is_empty())
  19. continue;
  20. builder.append(toupper(part[0]));
  21. if (part.length() == 1)
  22. continue;
  23. builder.append(part.substring_view(1, part.length() - 1));
  24. }
  25. return builder.to_string();
  26. }
  27. int main(int argc, char** argv)
  28. {
  29. if (argc != 2) {
  30. warnln("usage: {} <path/to/CSS/Properties.json>", argv[0]);
  31. return 1;
  32. }
  33. auto file = Core::File::construct(argv[1]);
  34. if (!file->open(Core::OpenMode::ReadOnly))
  35. return 1;
  36. auto json = JsonValue::from_string(file->read_all());
  37. VERIFY(json.has_value());
  38. VERIFY(json.value().is_object());
  39. StringBuilder builder;
  40. SourceGenerator generator { builder };
  41. generator.append(R"~~~(
  42. #pragma once
  43. #include <AK/StringView.h>
  44. #include <AK/Traits.h>
  45. namespace Web::CSS {
  46. enum class PropertyID {
  47. Invalid,
  48. Custom,
  49. )~~~");
  50. json.value().as_object().for_each_member([&](auto& name, auto& value) {
  51. VERIFY(value.is_object());
  52. auto member_generator = generator.fork();
  53. member_generator.set("name:titlecase", title_casify(name));
  54. member_generator.append(R"~~~(
  55. @name:titlecase@,
  56. )~~~");
  57. });
  58. generator.append(R"~~~(
  59. };
  60. PropertyID property_id_from_string(const StringView&);
  61. const char* string_from_property_id(PropertyID);
  62. bool is_pseudo_property(PropertyID);
  63. } // namespace Web::CSS
  64. namespace AK {
  65. template<>
  66. struct Traits<Web::CSS::PropertyID> : public GenericTraits<Web::CSS::PropertyID> {
  67. static unsigned hash(Web::CSS::PropertyID property_id) { return int_hash((unsigned)property_id); }
  68. };
  69. } // namespace AK
  70. )~~~");
  71. outln("{}", generator.as_string_view());
  72. }